Intro to Binding.Pry

Grace Tan
5 min readJul 7, 2019

--

Today marks the end of the second week of my software engineering immersive course at Flatiron School. The curriculum has been heavily focused on grasping the core fundamentals of Ruby including object orientation, class methods, class instances, working with enumerators, arrays, hashes, and object relationships.

Most, if not all of the ruby language I have been writing is in the Test Driven Development (TDD) environment. I am writing code to pass Rspec tests that have been written by someone else or myself. The reality that dawned on me as I approached the end of week two, is that the availability of Rspec and informative error messages cannot be assumed. For that reason, I found deeper enjoyment in writing and passing my own tests with Pry as my sidekick.

This gem of all gems (yes, gem install needed, pun also intended) is an essential tool for debugging, testing and just having a good old snoop into your code.

So, what is Pry?

Just like IRB (Interactive Ruby), Pry is a type of REPL (Read, Evaluate, Print, Loop). It’s awesome because instead of coding through IRB, you can code directly in your program and put in a ‘binding.pry’ anywhere in the code to evaluate the program.

Inserting ‘binding.pry’ into your code will freeze your program at the point of its insertion. When the program hits pry, the program will open a REPL and we get a chance to pry into our code and see what’s going on under the hood.

To use, first Install Pry gem

Run “gem install pry” in your command line.

Using Pry

I’ve created an example of a Party class containing class and instance methods.

I personally use “binding.pry” in two common instances, of course, there are many other situations you would use Pry.

Instance One: Prying into the value of an argument passed into a method.

Like, I wonder what “occasion” on line 19 is?

I put a “binding.pry” at the bottom so that I could pry into this particular method. Note “binding.pry” does not work if I put it inside my if/else statement, so the pry has been placed after the condition statement finished.

My example with some great arrow pointing action

Now, when I call on the “music” method in my program and run my file in the terminal, the Pry REPL will open. I can now test to see what the value of “occasion” from line 19 is by typing in “occasion” into my terminal.

Hey, it’s a “birthday”! Since we know it’s a birthday, we can be assured that once run, this method will return “Happy Birthday to you, Happy Birthday to you”. Sweet!

Instance Two: Prying into enumerable methods

In the example below, the Class method “all_party_types” will collect all party occasion types and return an array of all occasions that have ever been created in the Party class.

Because pry is an awesome debugging tool, it lets us look into the values of almost anything we want. For example, what’s inside “Party.all” (line 31), or “party” (line 31) or “party.occasion” (line 32)? Does it give us the data we expect?

If we type what we want to pry into, the REPL will print exactly what it is. See [1], [2], [3] below. Note that “item” and “item.occasion” only shows us one instance of “Party”, but if we hit “exit” into the command line and re-run those commands, the REPL will print out the next instance in the loop and show us the next value in the array.

Pry Tips

Because teamwork makes the dreamwork, here are my top four tips on using Pry.

  1. Require then Call on the method before trying to pry

You need to “ require ‘pry’ ”at the top of your file and then call on the method first before “binding.pry” will work.

2. Prying into a loop (“exit”)

You can use “exit” to pry into the return value of the next loop.

For example, if “Party.all” contains an array of 3 instances of “party”, typing “exit” into your command line will move onto the next instance of the loop. For each instance of the loop, you can access the “party” value. Magic!

3. Force exit out of the loop (“!!!”/”exit!”)

When you’re done with all that snooping around, you can hit “!!!” or “exit!” to get out of pry completely. “exit” without the “!” is also an option if you are not prying into a method with a loop.

4. Resetting pry (“reload-code”)

What’s the point of debugging if you can’t change your code and test on the go?

Once you’re done updating your code you can type “reload-code” in your command line. This will reset your pry session with the new program without having to manually exit out of pry and then re-enter pry after you’ve made changes to your program.

So, that’s all from me. Happy debugging!

--

--