debugging with ruby-debug

I wanted Rails-ish breakpoints in my standard, console-driven, unthreaded Ruby apps. For some reason I didn’t know how to do this.

ruby-debug

You will need to install the ruby-debug gem. Then, at the start of your application:

require 'rubygems'
require 'ruby-debug'
Debugger.start

Then to get a breakpoint:

debugger

When you run your code, you’ll drop into an old-school style stepping debugger at the debugger call points.

$ ruby some_app.rb
/Users/eweaver/p/some_app/some_app.rb:40: url = nil
(rdb:1) 

Now you can muck around in the prompt, kick over objects, cause havoc.

some handy commands

next     # run the next line and break
cont     # run to the next breakpoint
up       # jump up the stack one level, completing
         #   the current block
p        # inspect an object
display  # mark an object for automatic inspection
finish   # just run the rest of the code

It would be nice to be able to drop into an IRB instance, too, but I don’t know how to do that.

update

As of version 0.5.1, you can enter an IRB instance from the debug prompt by typing irb.

2 responses

  1. Discerning Rubyists use breakpoint?

    Now to combine them both into a stepping, IRB’ing, watch-point injecting megatron. Although breakpoint seems kind of finicky to me. I get fatal constant errors when I have more complicated loading interactions.