Skip to content

Instantly share code, notes, and snippets.

@nathanl
Created November 15, 2012 14:59
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nathanl/4079038 to your computer and use it in GitHub Desktop.
Save nathanl/4079038 to your computer and use it in GitHub Desktop.
Pry and debugger cheat sheet

Ruby Debugging

Pry

ruby -rpry some_script.rb - the r means require. If there's a binding.pry there, you'll be on it.

Pry gives you a graphical look at your program in progress, lets you cd among objects, ls to see available variables, etc. You can't step, though; just explore a snapshot at that moment.

Debugger

Must install debugger gem for your version of Ruby (1.9 is 'gem install debugger'). Doesn't play well with Spork.

rspec some_test_spec.rb --debug will stop on a debugger statement.

In a debugging session, set autolist on says "keep showing me graphically where I am."

  • l shows lines of code forward. Has flags:
    • - backward
    • = current line
    • X-Y lines X to Y
  • n is for "next"; "step over"
  • s is for "step"; "step into"
  • e some_statement evaluates that. If you want to do more, irb drops you into it.
  • hitting enter re-executes the last command; if you hit n and want to keep stepping, hit enter.
  • l18 displays line 18
  • Breakpoints
    • b file:line [if expr] sets a breakpoint
    • b class(.|#)method [if expr] sets a breakpoint
    • del [nnn] deletes some or all breakpoints
  • Displaying expressions
    • disp [expression] shows value of expression at every step
    • undisp [nnn] cancels display options
  • Examining the call stack
    • May have to call Debugger.start first?
      • where, up, down, frame
      • bashd.sourceforge.net/ruby-debug.html#FrameCommands
  • help is available

Debugger configuration

Put a .rdebugrc in the project directory or your home directory with:

set autolist
set autoeval
set autoreload
@banister
Copy link

Cool summary, however you can step with pry if you install the pry-debugger plugin, you can also engage in even more sophisticated types of debugging if you install the pry-rescue plugin. For a fuller look at pry plugins available, check out https://github.com/pry/pry/wiki/Available-plugins and the Pry Ecosystem posts.

@lfender6445
Copy link

updated cheat sheet for pry https://gist.github.com/lfender6445/9919357

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment