土曜日, 11月 07, 2009

Debugging Ruby and Rails

Using ruby-debug(Excerpt from P54-56, Advanced Rails, Brad Ediger, )
$ sudo gem install ruby-debug

▲To debug simple Ruby scripts, just run your scripts with rdebug rather than ruby
▲Debugging Rails with ruby-debug
  1. Require the ruby-debug library from config/environments/development.rb so that it is only loaded in the development environment:
    require 'ruby-debug'

  2. Insert a call to debugger anywhere you want to stop the application’s execution and drop into the debugger.
    class SignupController < ApplicationController
    def check_for_service
    debugger
    @query = params[:q]
    (...)

  3. Start up the Rails server. The debugger only works with WEBrick and Mongrel,
    because the running code still has access to the tty under those servers.
    Under FastCGI, the worker processes would not be able to interact with the
    console.
    $ script/server
    => Booting Mongrel (use 'script/server webrick' to force WEBrick)
    => Rails application starting on http://0.0.0.0:3000
    => Call with -d to detach
    => Ctrl-C to shutdown server
    ** Starting Mongrel listening at 0.0.0.0:3000
    ** Starting Rails with development environment...
    ** Rails loaded.
    ** Loading any Rails specific GemPlugins
    ** Signals ready. TERM => stop. USR2 => restart. INT => stop (no restart).
    ** Rails signals registered. HUP => reload (without restart). It might not
    work well.
    ** Mongrel available at 0.0.0.0:3000
    ** Use CTRL-C to stop.
  4. Interact with your application as needed to trigger the debugger. The request will hang in the browser, and the server console will drop into the debugger console and show the line of code it is paused on:
    app/controllers/signup_controller.rb:5 @query = params[:q]
    (rdb:1)

0 件のコメント: