If you are on Ruby 1.9.2+, install the gem install debugger . There are two ways to debug: directly enable the debugger gem or use the redbug binary code. Suppose we have a toy script and want to know why $blah is 4 after calling foo() (pretend it is an external library).
Method 1: enable debugger
This sets a breakpoint manually in your code:
require 'debugger' $blah = 3 def foo $blah += 1 end def bar $blah += 4 end foo() debugger()
Run this as ruby debug.rb . This will launch you in the ruby-debug console:
% ruby debug.rb debug.rb:15 bar() (rdb:1) list [10, 19] in debug.rb 10 $blah += 4 11 end 12 13 foo() 14 debugger() => 15 bar() 16 17 puts $blah (rdb:1) display $blah 1: $blah = 4
Method 2: Run rdebug
Here is an example of our sample script, debug.rb :
$blah = 3 def foo $blah += 1 end def bar $blah += 4 end foo() bar() puts $blah
From the shell, run rdebug debug.rb . Here is an example session:
% rdebug debug.rb (rdb:1) list 1,20 [1, 20] in /mnt/hgfs/src/stackoverflow/debug.rb => 1 $blah = 3 2 3 def foo 4 $blah += 1 5 end 6 7 def bar 8 $blah += 4 9 end 10 11 foo() 12 bar() 13 14 puts $blah (rdb:1) break 12 Breakpoint 1 file /mnt/hgfs/src/stackoverflow/debug.rb, line 12 (rdb:1) display $blah 1: $blah = (rdb:1) continue Breakpoint 1 at /mnt/hgfs/src/stackoverflow/debug.rb:12 1: $blah = 4 /mnt/hgfs/src/stackoverflow/debug.rb:12 bar() (rdb:1) display $blah 2: $blah = 4
The key commands are break LINE-NUMBER and display VARIABLE . Hope this helps!
Resources
jmdeldin
source share