Debugging in with “pry” in Ruby!

As a software engineer you will be spending more time debugging or looking for errors than writing codes. So let’s talk about a few ways you can try to check and debug your Ruby codes.

When I started my journey as a student at Flatiron school. I spent hours and hours sitting stuck with a bug in my code and had no idea how to check my code. Didn’t know much about the cool features Ruby has for debugging. This feeling can be very frustrating…

But, as I started working on my project I got more familiar with the ways to check and debug my code… So I wanted to share how you can debug if you are new to Software engineering .

So first let’s talk about REPL. What is REPL?

REPL stands for Read, Evaluate,Print,Loop. It is a program which allows you to type Ruby code and see the output of your code directly. Which comes in very handy if you are trying to find any errors in your code. Here is a website where you can print out your code or you can try different methods and see the outputs.

IRB is one popular ruby REPL which stands for “Interactive Ruby”. It is a quick way to explore the Ruby programming language and try out codes without creating a file. If you have Ruby installed in your computer you can start IRB by typing IRB and it will start a REPL, and you will be able to check code you want there. You can exit IRB by typing “exit!”

Now that we know how IRB works, let’s get to know what “pry” is.

What is “pry”?

Pry is another Ruby REPL like IRB but with more functionality and added features. It is more flexible than IRB. It is a powerful tool that Ruby developers can use to debug programs and push past hurdles. Like other Ruby gems, you will need to “require” it in your environment settings or specific ruby file in order to access its functionality. By requiring “pry” you will be able to use “binding.pry” which we will talk in a bit…

How to enter, test code, and exit pry

What is “binding.pry”?

Binding.pry is a life-saver when it comes to debugging. You put it here, there and everywhere, and you’re happier than you’ve ever been before. In simple words: “binding.pry” makes your life a lot easier!

Binding.pry is essentially ‘prying’ into the current binding or context of the code, from outside your file. So when you place the line binding. pry in your code, that line will get interpreted at runtime.

In the example below you can see I have an array of number which is set to a variable “a”. Also, I am using map to iterate over each number by 2.

In the picture below I put a binding.pry below “a*2” to see what “a*2” is giving me so far.

Once the binding.pry is inserted into the block, the code must be executed again in order to open the Pry console. When the Pry console opens in our terminal, we are within the block and now you will able to test variables and get return values:

This is the best part about “binding.pry”. I had the hardest time understanding pry until I used it more and more. Pry improved my ability to debug Ruby programs and is the tool that I always turn to when I’m stuck now. It is okay if you don’t understand at first, just try to use more and more. Just remember if you are stuck, just put a binding.pry in it, and see your code work!

HAVE FUN “binding.pry”-ing!

Junior Full-Stack Web Developer. Looking for opportunities!