Sometimes it’s nice to have a simple breakpointing function that will dump you into an interactive session with all your local variables in place.
There are more sophisticated solutions for the world of multiple servers and daemonized code, but after some fighting with IRB, I find myself using this little snippet of code in many projects:
require 'irb'
module IRB
def IRB.start_with_binding binding
IRB.setup __FILE__
w = WorkSpace.new binding
irb = Irb.new w
@CONF[:MAIN_CONTEXT] = irb.context
irb.eval_input
end
end
## call me like this: breakpoint binding
def breakpoint binding; IRB.start_with_binding binding end
As the comment states, you can invoke the breakpoint at any point by inserting
a breakpoint binding statement anywhere in your code. Once that line is
reached, you’ll be dumped into an IRB session with local variables intact.
Quitting the session resumes execution.
Obviously with this method I’m having you pass in your binding explicitly. There are fancier tricks for capturing the binding of the caller (involving kernel trace functions and continuations), but I’m opting for the simpler solution here.
Works with Ruby 1.9, of course.