I’d been wanting to write a post for some time on improvements I’ve found useful in using Ruby’s debugger. Friend Trevor Rosen beat me to the proverbial punch, however. Give his post a read first, I’ll wait.
Now on top of his suggestions I have one more refinement to add. I’ve defined a snippet in vim for whenever I need to use the debugger. You should be able to define the same in whatever editor you use (and if your editor can’t do snippets, get a real one). When in an .rb file, “debug” + Tab becomes:
1 | require 'ruby-debug'; Debugger.settings[:autoeval] = true; debugger; rubys_debugger = "annoying" |
In addition to the format Trevor suggested (which calls the necessary module, turns on auto evaluation, and calls the debugger itself) this adds a simple assignment after the debugger is called. I often find this necessary because I want to debug right at the very end of a given suite, a conditional, a method, etc. But if you invoke the debugger as the last statement in such a situation, it won’t actually be called. Here’s an example file:
1 2 3 4 5 6 7 8 9 | #!/usr/bin/env ruby def troubled_func(var1) puts "Entering troubled function..." secret_num = rand(10) * var1 require 'ruby-debug'; Debugger.settings[:autoeval] = true; debugger end troubled_func(5) |
When you run this, you won’t get a debugger console. All the output you get is “Entering troubled function…” and the script exits. If instead you add the assignment I list above after debugger is called, you will get to the debugger console. Sad, but true. (You can select a less offensive assignment if you wish. I settled on it in disgust.)
Aside from this, the Ruby debugger is a useful and beautiful thing. So add the snippet and forget about it.
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.








