ruby-debug in 30 seconds (we don't need no stinkin' GUI!)

Many people (including me) have complained about the lack of a good GUI debugger for Ruby. Now that some are finally getting usable, I've found I actually prefer IRB-style ruby-debug to a GUI.

There’s good tutorial links on the ruby-debug homepage, and a very good Cheat sheet, but I wanted to give a bare-bones HOWTO to help you get immediately productive with ruby-debug.

Install the latest gem

$ gem install ruby-debug 

Install the cheatsheet

$ gem install cheat $ cheat rdebug 

Set autolist, autoeval, and autoreload as defaults

$ vi ~/.rdebugrc set autolist set autoeval set autoreload 

Run Rails (or other app) via rdebug

$ rdebug script/server 

Breakpoint from rdebug

(rdb:1) b app/controllers/my_controller.rb:10 

Breakpoint in source

require 'ruby-debug' debugger my_buggy_method('foo') 

Catchpoint

(rdb:1) cat RuntimeError 

Continue to breakpoint

(rdb:1) c 

Next Line (Step Over)

(rdb:1) n 

Step Into

(rdb:1) s 

Continue

(rdb:1) c 

Where (Display Frame / Call Stack)

(rdb:1) where 

List current line

(rdb:1) l= 

Evaluate any var or expression

(rdb:1) myvar.class 

Modify a var

(rdb:1) @myvar = 'foo' 

Help

(rdb:1) h 

There are many other commands, but these are the basics you need to poke around. Check the Cheat sheet for details.

This can also be used directly from any IDE that supports input into a running console (such as Intellij Idea).

That should get you started. So, before you stick in another ‘p’ to debug, try out ruby-debug instead!

require 'rubygems'
require 'ruby-debug'
#$ruby breakpoint_test.rb
#vim breakpoint_test.rb
#:!ruby breakpoint_test.rb
#Next Line (Step Over)
#(rdb:1) n
#Step Into
#(rdb:1) s
#to debug in a function which is called from other place,before this function call,type 's' and you will found your are inside this function.
def leap_year year
leap = case
breakpoint
when year % 400 == 0: true
when year % 100 == 0: false
else year % 4 ==0
end
puts leap
end
if __FILE__ == $0
# tests...
puts "year=2000"
leap_year 2000
puts "year=2004"
leap_year 2004
puts "year=2002"
leap_year 2002
end

标签: breakpoint debug rails ruby shell