A small gathering of information about Ruby and debugging tools.
All debugging tools for Ruby ultimately exploit the same feature of Ruby – the ability of the Ruby interpreter to single-step through code and set breakpoints. Usually, two pieces are involved – the copy of the Ruby interpreter running the Ruby code in debug mode and the program issuing debug commands (via an established interface).
Ruby comes with its own command-line debugger – rdebug. In this case, rdebug acts a shell that sends debug commands to the Ruby program executing in the same instance of Ruby. It involves a little trickery, but is no different in concept to any other debugging technique.
IDEs and other tools use essentially the same hooks, accessed through interfaces such as ruby-debug or ruby-debug-ide. In these cases, the IDE presents the source code and execution context of the program in a user-friendly fashion. When a user clicks on a breakpoint, or runs the program, the IDE sends an appropriate debug command to the Ruby interpreter running the application in debug mode.
When trying to understand why Ruby debugging sometimes doesn’t work, it helps to know that the following elements must all be in agreement:
- The version of the OS
- The Ruby interpreter and the debugging interface it makes available
- The version of RubyGems
- The library that provides ‘packaged’ access to the debugging interface (usually ruby-debug-ide or ruby-debug)
- The implementation of the Ruby debug support in the IDE
There is no magic solution here – a lot of googling and trial and error.
An example
It is instructive to set up an IDE to debug Ruby externally, connect to a command-line instance of the program being debugged, and watch the debug console.
In a console window, in the directory containing the program, issue a command similar to:
rdebug-ide _0.4.9_ -p 7000 -d -- mongrel_rails start
In the IDE of choice, open up the application and attach to the remote debugging session – in this case running on localhost on port 7000.
Issue breakpoints, run commands, variable inspections in the IDE and watch the activity in the console window.