PRY (A debugging tool for Ruby)

Arpita Dutta
3 min readSep 14, 2020

Pry is a powerful debugging tool for junior ruby developers. It is a ruby gem. Pry is primarily the work of John Mair(banisterfiend). I came across pry during pre-work at flatiron school and ever since then , I have used it to see the return value of my code. It helped to clearly understand how my code was working. Pry freezes the program so that I can get inside my code and find out what’s going on and what needs fixing. Both IRB(interactive Ruby) and Pry allows REPL(Read-Evaluate-Print-Loop) commands but Pry uses color-coded syntax which makes mistakes easier to spot and code easier to fix.

How to install Pry

We can add pry by adding

gem ‘pry’

in our gemfile and run

$bundle install

or it can be installed by running

$gem install pry

in the terminal or command Prompt.

How to use Pry

To use pry in our program, first we need to put a

require ‘pry’

At the very top our program. Then we need to would put a

binding.pry

At the top of the code piece that is causing trouble and the run the code and it would freeze the program right where the binding.pry is placed and open up a pry console just like IRB.

Now we can write our code inside the pry console and see what it returns. We can even test out other code pieces in pry relevant to the program and see the return value.I would sometimes try to write some loops or conditional statements inside the console and then after getting the return value I was looking for , I would put it inside my program knowing that the code I put there would not cause any error.

Pry not working

If for some reason my pry is not working and we are not able to open up the pry console, it only means that there is a syntax error either a comma , a dot or in my case mostly unexpected end of input expecting an “end” (which mostly means a loop , a conditional statement or a method is missing an “end”). After fixing all the syntax errors if we are still not able to open up the pry console then we can put a binding.pry on the code above and try to figure out where exactly the code is failing and fix that. We can put binding.pry in multiple places in the program but it would only hit the first binding.pry in the program. If for some reason in the pry console you are getting a continuous list type “q” to quit the list. If you have more than one test case being passed in your program type “exit” to see all the test cases being passed or all the inputs your program is getting. To get out of a pry console type “!!!” Or “exit!” .

Reference

https://www.honeybadger.io/blog/debugging-ruby-with-pry/

--

--