Pry – IRB’s Smarter Younger Brother

Flatiron School / 12 October 2013

The following is a guest post by James Tong and originally appeared on his blog. James is currently in the Ruby-003 class at The Flatiron School. You can follow him on Twitter here.

Pry is one of my favorite gems. It is a REPL (it reads, evaluates, prints, loops). Pry is a replacement for Ruby’s default REPL, irb. Pry’s advantages over irb are Pry adds additional features such as syntax-highlighting, auto indenting, and super useful debugging features. To install pry and its plugins (in this guide, I will also be using pry-byebug), perform the following commands:

Once you have those gems installed, you can enter into a session with pry, by just typing pry in your Terminal.

My absolute favorite feature of pry is its ability to load any portion of a file you are working on in your text editor directly into your REPL session. Let’s say you are working in sublime text 2, so below is your prime.rb file:

If I wanted to play around with these methods, I normally would have had to copy and paste everything into my REPL session. Right now, this doesn’t seem like a big issue for my file as it is literally less than 10 lines long. However, imagine if I had a file that contains thousands of lines. Try copying and pasting something like that into your terminal (Don’t actually try it, it may take a while).

So how does pry tackle this problem?

So how do we use binding.pry?

The way you do this is by requiring the pry gem at the beginning of the file and then use binding.pry wherever you want to REPL to load into.

(notice how we put “binding.pry” after our first method “prime?(num)”; this causes the repl to only load the first method. If we wanted to play around with the “every_prime(num)” method, then we would have to put change the location of “binding.pry to the end of the file.).

All we need to do now is type “ruby primes.rb” in our terminal.

Ok, so we’ve went through pry a little. Now, let’s go through pry-byebug(use pry-debugger for ruby versions before 2.0). We use pry-byebug in much the same way as using pry. Pry-byebug is really useful if you want to step through loops. Pry lets you see what certain variables are equal to at different points of your code.

To realize the full utility of pry-byebug, let’s take a look at slightly more complex code.

What pry-byebug allows you to do is to step through your code. Notice how I put my binding.pry in the middle of my method. I do this so that when I open my file I go to that specific line of my code. I use pry so that I am able to see what variables equal at that specific point of code. Now open your ruby file again normally:

You will end up with something like this:

First type ‘q’ to enter back into your pry session.

You can see a group of your local variables/methods with the ‘ls’ command. You can even type in the name of the variable to see what it is currently defined as (shown below).

In this pry session, some other commands that you may want to try on your own are ‘help’, ‘cd’, ‘quit’, ‘step’, ‘next’.

Other resources

Watch this video Pry by Railscasts
Read more about pry at Pry github repo
Read more about pry-byebug at Pry-byebug github repo

Thanks for reading!

Ruby: Just Set It. Just Say It. Previous Post Winning Arguments. Next Post