Where to Put ‘binding.pry’ When Debugging Your Ruby Code

Debugging. It’s an unavoidable part of life as a programmer. Whether you have 15 days of programming experience, or 15 years, at some point you’re going to run into bugs in your program. Thankfully, there are several debugging tools at our disposal to help make our lives a little easier as we’re coding.

With that being said, if you’re coding in Ruby, you’re probably going to want to get comfortable with using the Pry debugging tool. But if you’re anything like me when I first started coding in Ruby, you probably haven’t fully grasped the power, or the purpose of this incredible tool. Well, fear not fellow Ruby newbie! Here’s where your REPL debugging skills level up.

If you’ve never heard of Pry, it’s a Ruby REPL tool that allows you to pause your code at a specified line as you’re running it. When you pause your code at that specified line, you’ll have access to the methods and variables that come before it. Check out the documentation when you get a chance.

Now to use Pry, you’ll first have to install it in your environment. You can either put gem ‘pry’ in your Gemfile and bundle install, or manually type gem install pry into your terminal.

After we’ve installed it, then we’ll have to require it in our environment with the line require ‘pry’, write binding.pry on the line we want our code to pause in, and then run our code. Let’s look at this simple example.

After you run your code, you’ll enter into an interactive environment in your terminal where you can access and manipulate your data. Super simple, right? But how do you know what line you should place a binding.pry ?

Well that depends on what you want to find out. But, a good rule of thumb would be to place it after the section of code that is taking in, or manipulating the piece of data that you’re curious about.

For example, say we wanted to know what string was currently be taken in by our add_homie method. To find this out, we would place our binding.pry at the very beginning of the method definition. We can see in the image below that when we type string into the pry console, we see the string storing “Hey what’s up” is being taken in.

Here’s another example. Say we want to find out what the add_homie method is going to do to the string we are passing into it. Since the string is being manipulated within the conditional statement in the method, we should probably place a binding.pry directly after the conditional statement.

Now, when we type string into the pry console, we can see how our conditional statement has changed our string value in real time. We went from “Hey what’s up” on line 7, to “Hey what’s up, Homie” on line 13. How cool is that! Think of all the inferences we can make from the information we can find with pry!

Knowing the when’s and where’s of your code can help the debugging process immensely, and Pry gives us a quick and easy way to do just that. I hope you found these examples helpful, and that you practice using Pry the next time you’re trying to squash a bug and get stuck. I promise you won’t regret it!

Software Developer. Never Stop Being a Student.