Ruby’s binding.pry: A beginners guide

James Sapp
5 min readJan 3, 2021

Are you new to learning Ruby? If so, learning Pry & how to read errors will help bridge the gap. If you’re like me, I’m the I like to work smarter not harder type. Pry helps for efficient programing & beginner friendly learning.

At first, I was very confused when I tried Pry in my prep-work for the Flatiron Software Engineer Program.

Thankfully, I’m not one to give up so I rolled up my sleeves and did some google searching and tried again. After some more practice it clicked! I then asked myself why was this so hard to learn? In this article I’m going to explain Pry and how to better understand it’s capabilities for an early on developer.

What is PRY?
“Pry is a powerful alternative to the standard IRB shell for Ruby. It features syntax highlighting, a flexible plugin architecture, runtime invocation and source and documentation browsing.” — source

Pry is another Ruby REPL with some added functionality. REPL stands for Read, Evaluate, Print, Loop. It is an interactive programming environment that takes a user’s input, evaluates it and returns the result to the user.

What does this mean? Pry is essentially your right hand man when it comes to debugging & reading errors. Before you learning more about Ruby, I would first suggest mastering pry. It is a fundamental tool in helping you understand the Ruby language.

How does Pry work?
First, In your terminal check to see if pry is installed:

This should display in terminal [1] pry(main)>
You are now on line 1 of the pry console and you can start writing Ruby code.
Try it for yourself! When you’re done type exit to leave the console.

binding.pry is a single line of code you can place inside any method to pry into the code block & review results. Let’s get this set up.

  1. Inside yourfilename.rb file on line 1 type — require ‘pry’
  2. Require ‘pry’ must be at the top of any yourfilename.rb files
    when calling on binding.pry
  3. In your terminal type ruby yourfilename.rb to run the file

Using binding.pry inside a method
binding.pry
is a single line of code you can place inside any method to pry into the code block & review results. Let’s get this set up. Inside the pry you can try out new methods till you get the desired results.

Tip: To use binding.pry in a method you must use do..end syntax to write your methods, you can change them back to {} afterwards if you so choose.

In this method, we want to return the names of the spicy foods from the array of hashes. We have found our first hash. Now, let’s go one more step to find just the name.
Now we have the name of a spicy food! Great, but their might be more names? Let’s find out.

In the example below, we can now loop through any collection of data to see the elements we want to return. Typing exit will loop to the next element in the collection. We have three elements in our collection we will need to type exit three times to see all of your returned results.

Now we have all 3 types of spicy foods returned in an array. High five!

Tip: If you need to exit of a loop type !!! (triple bang) to hard exit
the pry console.

Wait!! Pry isn’t working? binding.pry needs to be placed inside the code block to invoke the pry console in the terminal. After the code block means the code has already run and there isn’t anything to pry into. Move your binding.pry to inside your code block & run your file again.

Using binding.pry twice in the same file.

  1. Type your first binding.pry inside your method
  2. Type your second binding.pry as your second to last line of code. Put a 0 after it so that pry isn’t your last line of code. This is to ensure pry works on this file and doesn’t jump to another file.
  3. Run your file & you should see see binding.pry and the last line of code.
This takes us to the binding.pry on the bottom of the page.

Now, how do I to get to the pry inside the method?
Inside the pry console type the name of the method along with any arguments and pry will jump to the method holding binding.pry.

Use the same steps as previously mentioned to test the code block.

Pry commands:
exit
— has two uses
1) leave the pry console
2) “soft exit” to move to the next element in the loop

!!! — hard exit or triple bang
Use to exit any iterations and/or to hard exit the pry console.

How to troubleshoot Pry?
Read the errors
. It sounds simple, I know, but your next task is to become comfortable reading the errors. Pry returns errors that help guide you in a time saving direction to get your code working.

Here’s an article on reading errors in Ruby.

Lastly, why is Pry so important?
For myself pry was crucial in learning how to read errors & know where they are so I can find them, fix them. It’s a real time saver & informative tool. Pry takes the guessing game out of coding. Instead of spending x amount of time on one problem that could be fixed with using pry from the start.

Remember if at first you don’t succeed then pry, pry again!

--

--

James Sapp

Full-Stack Software Engineer, Flatiron School Alumni