Pry Harder: How to debug your Ruby program like a pro!

Felix Rodriguez
4 min readJun 3, 2020

Ever had your car breakdown? Who hasn’t right? Well, what do you? You take your car to the mechanics of course. And how do they figure out exactly what is wrong? Well, they have to pop the hood and take a look underneath. This is where the they can test the individual components and pinpoint the problem.

What does any of this have to do with programming you ask?

While learning to code in ruby I have had multiple “breakdowns” , but in software development, we call this a bug. In the simplest of terms.. the code you write does not always do what you expect it to do, hence a bug.

So how do you look under the hood of your program? Enter Pry!

In my journey to becoming a Ruby software developer, there has been no tool more valuable than Pry. I’ll tell you why that is, but first..

What is Pry and what can it do for you?

Well, according to wikipedia:

Pry is an interactive shell for the Ruby programming language. It is notable for its Smalltalk-inspired ability to start a REPL within a running program. This lets programmers debug and modify the current state of a system.

In other words, its a way for you to “pop the hood” on your program at any point you set, while it runs. This gives you the ability to step through each instruction in the program and view the state of the program at that point in time. It is a vital tool to help with testing and fixing bugs in your program.

So why has pry been so valuable in my learning experience and why would you want to use it?

Well for starters, its super cool and the abilities it gives you are so powerful. When it comes to writing code and truly understanding how my code is working, this tool has been key.

Another reason it is so valuable is because of how much time it saves you. I found it rather time consuming to rewrite my code or copy and paste it into the IRB shell just to test some small feature or to see how it works. Instead, with pry you can just set a breakpoint or a “binding” at the exact line of code you are trying to understand.

Pry just makes things so much easier.. take my word for it.

How do you use pry? The first thing you need to do would be to install the pry gem using the command:

Then to use it in your code you just need to require the gem in your ruby file and set a breakpoint in the program using the “binding.pry” instruction. It is also important to make sure that the method that includes your binding is actually called at runtime. Here’s an example:

Once you run your ruby file the code in your program will run and once it gets to the “binding.pry” instruction on line 7 it will stop running and drop you into an interactive shell.

This is where the magic happens. It is in this shell that you run commands to see what methods are available to be called, actually call methods, and view the values of any variables that are in scope. This is you “looking under the hood” of your program.

For example, in the image below you will see where the program is run and I typed in the name of the variable. This returned the value of that variable at that point in time in the program.

Some other useful command that pry offers are the following:

  • help - self explanatory and probably most important command to know
  • whereami - this allows you to see exactly which line of code you are at in the program
  • ls <object> - will show you the methods that are available to be called on the obect
  • cd <var> - will allow you to step into the scope of the variable you pass it
  • next - execute next line of code in your file
  • step - this will go onto the next function that is called as opposed to the next line of code
  • continue - this command will continue running the program until it reaches another breakpoint or “binding.pry”
  • exit-program - will quit the current pry session and continue

Those are just some of the most useful pry commands that I use on a regular basis. Pry can do so much more though. Here is a link to the pry Github repository where you can view to documentation to see what else you can do with pry.

Hopefully this can be as helpful to you as it has been to me in this journey to becoming a professional software developer.

--

--