Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1

54 | Chapter 2: ActiveSupport and RailTies


(rdb:1) n
.../gems/ruport-1.2.0/lib/ruport/data/table.rb:806 case(args[0])
(rdb:1)
.../gems/ruport-1.2.0/lib/ruport/data/table.rb:807 when Array
(rdb:1)
.../gems/ruport-1.2.0/lib/ruport/data/table.rb:810 when /\.csv/
(rdb:1) c
$

The debugger has many other commands and features. Breakpoints can be set and
cleared on arbitrary lines or methods, and they can be conditional:


(rdb:1) break table.rb:805 if args.first =~ /\.csv/
Set breakpoint 1 at table.rb:805
(rdb:1) c
Breakpoint 1 at table.rb:805
.../gems/ruport-1.2.0/lib/ruport/data/table.rb:805 table=
(rdb:1)

Theirbcommand (still experimental) will start up an interactive Ruby session
within the context of the current code, allowing you to examine the environment:


.../gems/ruport-1.2.0/lib/ruport/data/table.rb:805 table=
(rdb:1) irb
>> pp args
["time.csv"]
=> nil
>>

Thehelp command will show you information on the available command lan-
guage—there are several more commands and options available.


Debugging Rails with ruby-debug


Now that we understand the basics of the debugger console, we can see how it inte-
grates with Rails. The process is quite simple and easy; ruby-debug is built to be used
with Rails with virtually no setup.



  1. Require the ruby-debug library fromconfig/environments/development.rbso that
    it is only loaded in the development environment:
    require 'ruby-debug'

  2. Insert a call todebuggeranywhere you want to stop the application’s execution
    and drop into the debugger.
    class SignupController < ApplicationController


def check_for_service
debugger
@query = params[:q]
(...)


  1. Start up the Rails server. The debugger only works with WEBrick and Mon-
    grel, because the running code still has access to the tty under those servers.

Free download pdf