MarkUs Blog

MarkUs Developers Blog About Their Project

Debugging in Rails

without comments

Learning how to debug in Ruby on Rails is arguably one of the most important things to learn when getting started with rails development, yet isn’t always easy to pick up with the documentation available. What follows is an introductory guide to rails debugging, with the goal of getting new developers up and running with the command-line debugger as quickly as possible.

Logging

One of the simplest ways to debug in rails is to simply output information to the screen. This is especially convenient when it is necessary to continuously output the value of a variable over time.

The easiest way to do this depends on where you are debugging – whether it is within a view, controller or model.

Views: DebugHelper

The Rails ActionView comes with a debug helper that makes debugging in a view easy.

Within a view, simply enter debug followed by the name of the variable of interest to print out the variable in a human-readable format. For example:

<%= debug @current_user %>

The above code will display a detailed YAML output of the given variable, in this case @current_user, at that particular point in the execution:

--- !ruby/object:Admin 
attributes: 
 first_name: admin
 section_id: 
 notes_count: 0
 last_name: admin
 updated_at: 2013-01-19 01:05:27 Z
 created_at: 2013-01-19 00:56:14 Z
 grace_credits: 0
 hidden: 0
 api_key: MjBjZDk1Y2Q5MjdhZTU1MjE2NjBlYTNmYmQzODY4Zjc=
 id: 1
 user_name: a
 type: Admin
attributes_cache: {}
changed_attributes: {}
destroyed: false
marked_for_destruction: false
new_record: false
previously_changed: {}
readonly: false

Controllers and Models: Logger

Within a controller, it’s still possible to pass debugging information to the screen by passing it to a view object, although it is typically simpler to log it to the console. This can be done using the default rails logger, which takes a string as input. Duplicating the above example using the logger:

logger.debug "Current user: #{@current_user.inspect}"

In this example, the inspect method is used to convert the contents of the @current_user object into a human-readable format, which is then output to the console by the logger. Note that the console output is also logged to a text file for later viewing. In the Markus development environment, this is saved in logs/development.log.

Debugging

For more complex problems, it is often necessary to step through the code to get to the root of the problem. This can be done using the built-in ruby-debug gem.

The ruby-debug gem must be first installed in the Markus environment, which can be done with the following command:

sudo gem install ruby-debug

The debugger can then be invoked by entering debugger anywhere in the ruby code, which has the effect of creating a breakpoint. When the debugger statement is reached, the application is paused and creates a debugger shell in the terminal.

Some of the most important commands include:

  • backtrace – Display a stack trace of the application execution.
  • break – Set new breakpoints, at a particular line or method in a file/class.
  • catch – Tells the debugger to catch all exceptions of a particular type, or (with no arguments) catch all exceptions.
  • continue – Resume execution.
  • delete/disable – Delete/disable a breakpoint.
  • display – Used to set the debugger to display the value of a certain variable each time you move the debugger to a new point.
  • down/up  – Move up/down n levels in the execution stack.
  • finish – Continue execution until the current (or specified) stack frame returns.
  • frame – Used to move to any point in the execution stack. Call frame n, where n is the number listed in the backtrace.
  • irb – Start an interactive ruby interpreter. Interactively execute code within the current application context.
  • list (or l) – Display the lines of code surrounding the current line being executed. Can be used in succession to display following lines of code. Use list- to display previous lines of code.
  • method – Display methods available to an object or class.
  • next/step – Steps through the code, one line at a time. Next is more like “step over” and step more like “step in”. Use next/step- to move backwards.
  • p or pp – Print (or pretty print) the value of a ruby expression.
  • quit – Exit the debugger shell. Also will terminate the server.
  • reload – Reload the ruby source. Required to reload the code if you change it while the debugger session is running.
  • thread – Show running threads, switch between them, and stop/resume.
  • var – Display all variables of a certain type (gloabl, instance, local, constant) and their values.

For the specific parameters of these commands, enter help <command> in the debugger shell. Simply entering help will provide a list of all commands.

Written by Mike Stewart

April 14th, 2013 at 1:50 pm

Leave a Reply