Pry

Using PRY

Saief Sayed
4 min readNov 6, 2020

As I embarked upon my journey at Flatiron School, I found myself scratching my head to figure out where I was going wrong. I discovered pry in my pre-work and learned that I can use it to ‘pry’ into my code to see what was going on. However, when I tried to use it initially, many times I would not hit pry and I got discouraged from using it. But I kept experimenting and later on when I figured out how to use it properly, I began to appreciate the power of pry. In this blog, I will give a short overview of pry and explain the ways we can use it to help us be better coders.

What is Pry?

‘An IRB alternative and runtime developer console’

We can utilize it by installing the pry gem with ‘gem install pry’. We need to add ’require ‘pry’’ at the top of our ruby file or add it in our ‘environment.rb’ file of our app. Once that is set up, we can insert binding.pry to some place in our code. When we run our code in the terminal, it will stop our code where we put the binding.pry and will throw us into a pry session where we can test our code. Let’s look at it in detail further.

Where to place binding.pry?

Suppose you are writing a method that takes in a parameter, you can place binding.pry on the line right after you define the method with ‘def’. You can type the name of the parameter in your pry session to see exactly what is being passed in.

Screenshot of an excerpt of my program in pry session

The above image is an excerpt of my project in pry console. I am testing a method ‘get_workout’ which accepts m2 as a parameter(m2 is an instance of a user class which is coming from another part of my program). I placed my binding.pry right after the method declaration. As I ran the program in my terminal and got to the place where it invoked the method, my program stopped and threw me in a pry session, which is also sometimes referred to as ‘hitting the pry,’. After I hit the pry, I typed in m2 and it showed the instance class. For further testing, I called the associations related to the instance of the class and iterated over that in an enumerable, to test what this method would return. In short, I typed in pry what I would type in my method to test if I can get the desired output.

What happens if I don’t hit the pry?

One of the infuriating things about using pry in the beginning was, I would not hit it. The reason I would not hit it, is because the code I had before I placed binding.pry was faulty. Since our code is being read from top to bottom, if we have faulty code before where we placed binding.pry, our error message would be executed and would exit out of the program and print the error message to our console instead. We need to move up our binding.pry before the faulty code. If we still get an error, we need to move it up more. If we hit our binding.pry and get thrown into a pry session, that means our code up to that point doesn’t have errors. That is how we can use binding.pry to debug our code and get insight about our code.

Exit out of pry and additional binding.pry

To exit out of pry, we can type in exit in your terminal, and that would take us out of our pry session. We can also place additional binding.pry in our code. This comes in handy when we want to test two or more portions of our code. So say for instance we have two methods, and our second method is dependent on our first method. We can place binding.pry in our first method and in our second method. If we hit pry in our first method, and ‘exit’ out of it, our code will continue to run and hit pry in our second method, assuming there were no errors in the section of code in between the two binding.pry. To exit out of our second pry session we can type exit in our terminal again. If for example, after we hit our first pry session, we do not want to hit the other binding.pry that we placed in our code, we can exit out of the whole session by typing !!!.

Conclusion

I hope that this blog was helpful for new coders to implement pry in their debugging and developing endeavors. I found pry to be a very helpful tool and I would recommend people to use it whenever they can. A more detailed explanation and the gem file location is referenced below.

References

--

--