Learning Ruby

Reference, Mnemonic & Ramblings

Debugging Ruby Code With the Pry Gem

This is a quick reference guide for using ‘pry’ gem for debugging Ruby code.

Installation:

  • gem list pry: lists whether it is already installed
  • gem install pry: installs the pry gem

Use:

  1. require 'pry'
    • insert the above line at the top of the ruby file you intend to debug
  2. binding.pry
    • insert the above line to stop execution at a particular point in the code
    • insert this after the line where you want to pause the program execution
  3. run the program
    • type the debugging stuff (variable names, method names etc.) once the program pauses
  4. step
    • type ‘step’ to go to the next line
  5. Ctrl+D
    • use the above key combo to move to the next ‘binding.pry’ statement in the program

Here’s a good screen cast describing the debugging features of Pry.

However, debugging is just one of the many things that can be done using Pry. One other main feature is replacing IRB. A good reference page (with screen-casts and all) can be found here.

Other useful links:

comments powered by Disqus