This post originated from an RSS feed registered with Ruby Buzz
by Obie Fernandez.
Original Post: Poor Man's Breakpoint
Feed Title: Obie On Rails (Has It Been 9 Years Already?)
Feed URL: http://jroller.com/obie/feed/entries/rss
Feed Description: Obie Fernandez talks about life as a technologist, mostly as ramblings about software development and consulting. Nowadays it's pretty much all about Ruby and Ruby on Rails.
I immediately figured out that you have to pass in the binding (or context) of the code where you want to breakpoint, which frankly is no big deal. I never used the arguments to breakpoint anyway. Without too much trouble, my first attempt worked, albeit crudely.
defbreakpoint b
while !(line = gets).strip.eql? 'exit'begin
puts eval(line, b).inspect
rescue
puts $!nextendendend
Then Zed suggested that I use readline instead of gets, so my hack would understand Ctrl-A and have a buffer history. That worked pretty great, and I'm sure someone could take it further and add completion.
require'readline'includeReadlinedefbreakpoint b
while !(line = readline("> ", true)).strip.eql? 'exit'begin
puts eval(line, b).inspect
rescue
puts $!nextendendend
Finally, I started thinking that it'd really be nice to just fire up IRB instead of my hackish while/gets/eval routine. Which ends up being a very simple solution indeed.
require'irb'defbreakpoint b
puts "Executing breakpoint at " + caller.first
IRB.start(nil, IRB::WorkSpace.new(b))
end
To use just throw the code into an .rb file in your lib directory and require it at the bottom of your config/environment.rb file. Then type breakpoint binding wherever you want a breakpoint. Note: I only use breakpoints in tests and one-off Ruby scripts. What I've detailed here is no replacement for the DRb-based breakpointer code in Rails.