Lately, I’ve been spending some time in Rails. As you know, no code is 100% bug-free … in most cases, you can figure out errors/bugs by checking the log files, present in your log directory or by checking the traceback.

What do you do when you need to step through your code? I searched for some solutions on the internet, and I asked on stackoverflow. I didn’t receive any answers to my question, but, I remembered a gem I was using in my ruby code: ruby-debug. This gem allows you to stop your script at a given moment and step through the rest of the code.

I’m sure most/all of you know how to install a gem. If not, here’s the command:

gem install ruby-debug

After you install the gem, you include it in your code like this :

require"rubygems"require"ruby-debug"

You need the require “rubygems” line if you install the gem through ruby’s gem management system. After you included the gem in your source file, you can place “debugger calls” inside your code, like this:

When the ruby interpreter reaches the debugger line, you’ll be dropped to a irb-like shell, from where you can step through your code and inspect your variables. A small catch: the shell you get back isn’t a full-featured REPL. If for example, you write the name of a variable and expect it’s value to be printed, it’s not gonna happen. The gem treats it like a command, so, you need to say p name_of_variable or puts name_of_variable .

I suggest you take out those “debugger” lines after you’re done debugging.

If you know of other solutions to this type of problem, please mention them in a comment.

I’ve been debugging on some code, and I thought it would be great to be able to decorate functions that would perform something when called with a desired argument. I asked about in stackoverflow, and MizardX provided me with a great solution ( which I’ve hacked a bit ). Here it is :