Ruby Debugger

Thursday, October 12, 2006 by Nate Murray.  

I just started to learn the ruby debugger. Its pretty helpful in those cases where puts just doesn't cut it. I found a good introductory article on this over at IBM: Debugging Ruby programs 101 (registration required). One of the most annoying things was that there seemed to be a catchpoint on every single exception that was raised. I had to keep hitting c just to get to my first actual breakpoint. I found the solution to this problem over at comp.lang.ruby. Basically, there is a default catchpoint on StandardError so you are prompted at all raise exceptions. What you need to do to fix this is just set a catchpoint for your own type of error. So when you start up simply:

  cat MyOtherError

It will remove the default catchpoint and catch only on MyOtherError.

Another helpful Ruby Debugger tip is to have the debugger load a debug.rc file on startup. Matthias Georgi posted this hack on comp.lang.ruby that could be helpful.

A short hack.
Make a copy of debug.rb and put these lines right after the Context

class Context
  alias original_readline readline
  def readline(prompt, hist)
    @rc_file ||= File.readlines("debug.rc")
    if @rc_file.empty?
      original_readline(prompt, hist)

Now create a file debug.rc with your desired breakpoint:
b 100
b 200

Start the debugger:
ruby -r./debug myscript.rb 

Of course, this isn't a totally clean solution in that you are editing a standard file. Also, I don't think this code would work if the debug.rc file did not exist. Nonetheless, its the easiest method I've seen so far.


» Post a Comment

Who we are:
The Pasadena Ruby Brigade is a group of programmers who are interested in Ruby. We enjoy sharing our ideas and code with anyone who is interested in this wonderful programming language.

Who can join:
Anyone! Just head over to our mailing list and drop us an email.

What we do:
We recently started a project over at RubyForge. This project is a group of programs written and maintained by our members that we thought could be beneficial to the whole community.



Recent Posts