An Intro to Ruby Debugging featuring Pry

Dick Ward
4 min readJul 24, 2018

--

You’ve been working with Ruby for a little while now and you’re starting to write bigger, more complicated programs. Maybe you’re playing with Object Oriented programming and making classes, maybe you’re making a CLI project with a complicated runner file — wherever you’re at, it’s time to learn the basics of debugging with pry.

 array = [1, 2, 3, 4, 5] array.each do |number|   puts number  end

Chances are, if you’re not using a tool made for debugging, you’re using puts to help see what’s going on in your code. In the example above, we’re using puts to see what each number is as we iterate through our array. It’s simple and effective, but when data gets more complicated, it’s not the best option. Enter pry.

Pry is a Ruby Gem that can either be required in your Gemfile or installed with gem install pry. Require it at the top of whatever file you’re working in with require 'pry' and you’re all set up. Time to have some fun. Let’s start with an array that’s a little more complex.

require 'pry'person_array = [{name: "Tashawn", interests: ["sports", "tv"]}, {name: "Laura", interests: ["skiing", "books"]}, {name: "Evans", interests: ["sports", "camping"]}]

Here we have an array of three elements, which are hashes. Each of these hashes has two keys, and the value of the interests key is an array. It’s not the most complicated array in the world, but even going this deep, it can be easy to lose your way. We could use puts to display information while iterating through this array, but that assumes that we’re know what we’re looking for.

def say_hi(array)
array.map do |person|
"Hello #{person["name"]}!"
end
end

Let’s take the code above as an example. It’s currently broken, but we’re not sure why. When we run the code, the result is ["Hello !", "Hello !", "Hello !"], so we can guess that the problem has to do with person["name"].

But let’s not guess. Let’s pry in and find out. In order to use it effectively, we’ll want to put binding.pry in before the code that’s producing the error. We’ll also need to call the method we want to debug. What pry will do is pause our code as it is running, so that we can poke around and see what’s going on.

def say_hi(array)
array.map do |person|
binding.pry
"Hello #{person["name"]}!"
end
end
say_hi(people_array)

Cool. Now if we run the code, we should see something like the following:

IRB running into a binding.pry

The arrow on the left indicates that line 7 of our code is the next line that will execute. On the bottom we can see that we’re now in pry(main). Don’t worry too much about what that means for now — I’ll get into more advanced techniques in future articles. The important thing to know is that you’re inside your code.

Here’s where you can test your assumptions. Type array into your terminal, and it’ll show you whatever is being passed in as an argument. In this case, it’s our array of people. Type person and you’ll see whatever is at index 0 of this array. In this case it’s our hash with a name of Tashawn.

If you type exit, you may be surprised to find that you end up right back line 7. What’s going on here? Well, pry gets called every time it gets hit, so if you’re iterating through an array, it’ll hit for every single index of that array. Calling person now will get us the hash with a name of Laura. If you want to get all the way out, call exit! instead.

“Okay cool,” you may be saying, “but what does all that do for me that I couldn’t do with puts?” With pry, we can write code consequence free and test our assumptions in a way that we couldn’t with just a simple puts. Check it!

Testing assumptions in Pry

Just like in the gif above, we’re able to call person.name, person["name"] and finally person[:name], which turns out to be the correct syntax. We can now go back to our editor and make the fix.

We’re just calling person[:name] here, but we could do any number of things , including writing out whole methods to test out before putting it directly into our code.

Writing a new method inside of our pry

Pry is an absolute beast of a tool, and it can do so much more than we’ve worked on up until this point. Play with it, test your assumptions, and test its limitations. You’ll find that there’s a lot more you’d like to be able to do, there’s a lot more that pry is capable of. I’ll be going deeper into it in a future article, but don’t wait for me —look at the documentation and experiment yourself!

--

--

Dick Ward

Full-stack web developer with a flair for the theatrical and a mission to leave the world a better place than I got it. DickWard.com