6.7. Debugging

Debugging is another important aspect of developing Rails applications. When something goes wrong it's fundamental to be able to understand what exactly is happening, as well as evaluate expressions and easily move around within the code.

As a Microsoft developer, you are probably accustomed to the excellent debugging support offered by Visual Studio and .NET. Having programmed in both environments, I can confirm that the level of support and convenience for debugging offered by .NET was way beyond what the Ruby world had to offer.

Ruby In Steel, covered in Chapter 1, enables you to work with Rails in Visual Studio and provides a fast debugger called Cylon.

Fortunately, there have been many efforts to improve this situation and Rails 2.x really simplifies the process of debugging applications by taking advantage of the fast ruby-debug gem. To perform fast debugging in Rails, three things are required:

  1. ruby-debug needs to be installed.

  2. You need to specify a "breakpoint" where you want the debugging session to start.

  3. The Web server needs to load the debugger.

Let's start by installing the ruby-debug gem. Run the following:

gem install ruby-debug

Notice that I opted not to add this as a prerequisite in config\environment.rb because it's a gem that's required for development purposes only.

Having installed ruby-debug, you can now place the method debugger anywhere you want in the application. Let's temporarily add it to the index action of the Articles controller ...

Get Ruby on Rails® for Microsoft Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.