As a disclaimer, this guide is not meant to be an in-depth user guide to Pry, just a launching pad for those who don’t know where to start. This is the quick and dirty guide I wish I had early on to give me the basics for building a foundation. If you are looking for detailed guides and documentation, you can easily find a plethora of those on the internet. I’ll even throw in some links at the bottom of my article in case you want to dive in. Happy reading!
I honestly don’t even want to admit how long it took me to get on the Pry bandwagon, but I certainly regret not doing it sooner. Pry not only makes it easier to test code, it literally allows you to pry it open and see how it works under the hood — hence the name! Sure, you could keep using IRB (Interactive Ruby Shell) for testing, but aren’t you tired of copying and pasting snippets of your code over, and over, and over again? Wouldn’t it be great to actually see how things are working in your application rather than just guessing? This is where Pry comes in.
What is Pry?
Pry is a Ruby REPL that packs an awesome punch. REPL stands for Read-Evaluate-Print-Loop, and it’s a nifty, interactive programming environment that allows you to type in Ruby code, experiment with it, and see the results immediately in your terminal.
Alright, so what does Read-Evaluate-Print-Loop actually mean? In short, it means that your computer is doing four things in the following order:
- Reads your input (i.e., your commands, like
- Evaluates your expression (figures out what you are requesting and does it, like adding two integers)
- Prints your values (returns the computer’s response to the expression, like returning
4as the answer to
- Loops back to the beginning (i.e., loops back to step #1 and waits to read your next input)
So IRB is a REPL, why should we bother with Pry? Well, there are a number of key features that make Pry a more robust and powerful tool than IRB, but the main reason comes back to the ability to get right inside of your code and see exactly what's going on at a given time. Think of it as freezing time and being able to experiment with all of the objects around you, one at a time, in the context of their environment.
How to Install Pry
Unlike IRB, Ruby’s built-in REPL, Pry is a Ruby gem meaning that you have to install it first to use it. This can be easily done by typing
gem install pry in your terminal:
gem install pry
Once you have Pry installed, you will need to ‘require’ it in whatever Ruby file you wish to test. As you get more advanced, you can do a fancy set up in your environment to make it accessible everywhere, but as a beginner, it’s totally fine to just start using it one file at a time.
Setting up Pry
To set up Pry in your code, drop in
require 'pry' at the top of your file and a
binding.pry, or a breakpoint, wherever you want your code to stop for testing. If you are just starting out with Pry, I recommend placing the
binding.pry at the end of your code so you can have access to everything. Once, you’ve got a good feel for it, go nuts and drop breakpoints wherever you need them, as many times as you need them.
Now that we’ve got it set up, simply run your ruby file in the terminal, e.g.,
ruby./lib/test.rb, and you should hit the
Once in Pry, you have every bit of code before the breakpoint at your fingertips for testing without having to type it in the terminal. Pretty awesome, huh!?
Finally, to exit Pry, simply type
Once you get a handle on Pry, one of the first places you’ll want to start placing breakpoints for extra support is within methods, specifically those that iterate over arrays or hashes.
If you drop a breakpoint inside an iteration, you now gain the power to see what is going on each iteration. AMAZING!!!
In the gif above, you’ll notice that I dropped my breakpoint within an iteration on line nine. When I initially hit Pry, my value for
i on line eight is
1. When I type
exit, I am still hitting the same breakpoint, but now I’m in the second iteration of my expression. How do we know this? By testing
i again and seeing that the return value is now
2, the second element of my
To see what the next iteration will look like, I can keep typing
exit in my terminal and checking my block parameter. Eventually, an
exit will break me out of Pry entirely.
Final Words of Wisdom
If you drop a breakpoint inside of a method and run your file, you might not hit that
binding.pry as expected. Don’t freak out! Remember that there is a difference between defining a method and invoking it. Your
binding.pry might be in the definition, but unless you invoke the method, it’s not going to hit the breakpoint. This can easily be solved by invoking the method after it is defined.
Now that you know how easy it is to get started in Pry, you’ll become a wiz in no time. I’m a firm believer that the first step is 85% of the journey, so congratulations on getting started!
Below are some great links on Pry that go into the weeds of all it’s amazing features: