Pry - Getting to The “True Knowledge”

Mike Archer
5 min readSep 21, 2020

Fixing broken code can be an absolute pain, to make it worse, sometimes as a new coder you do not even know where to begin. I embarked on my journey into coding through the Flatiron School program with no prior experience in the tech world. Immediately, I found myself outmatched as I worked through coding exercises meant to show me the way, often struggling to understand even the most basic concepts. Thankfully, a few lessons in, I was thrown a life saver, or so I thought.

The curriculum told me about “binding.pry” or Pry, a Ruby gem that could be used to “pry” into your code as it executes and see what is happening. As I progressed through my pre-work the next few weeks I occasionally experimented with pry, but looking back, I had no idea of the power and time-saving ability that had been given to me. It was not until the first week of class, when one of our instructors introduced us to the Fake-Billboard topping jam, “Hit The Pry”, that I was able to really begin to leverage pry in my favor.

What is Pry?
“Pry is a runtime developer console and IRB alternative with powerful introspection capabilities. Pry aims to be more than an IRB replacement. It is an attempt to bring REPL driven programming to the Ruby language.”

In reality, there is no magical song (yet?) to help you understand how to use pry. Like most things, simple repetition and experimenting is the best way to familiarize yourself with this gem. I once heard someone say “True knowledge is not knowing that the grass is green. True knowledge is understanding why the grass is green.” Proper use of pry can bring you this “true knowledge” in regards to your code. It is never simply enough to know that your code is failing, and when you are just beginning it can be daunting to even find the path to follow that will lead you to you error. Once located, it can be even harder for a new coder to fix these mistakes. This is where Pry comes into play. When you use pry, you are essentially stopping time and “prying” into your code. This can be tremendously helpful when learning enumerators, trying to see the value of a variable, learning the basics like indexing arrays, and most importantly, giving you insight into why you are not getting the expected results.

Imagine you had poorly labeled variables…

“…True knowledge is understanding why the grass is green.”

Pry Session showing ‘a’, ‘b’, and ‘c’ values

Set-Up & Implementation

First things first, you need to install the Pry gem, this can be done a few different ways, the easiest of which is to simply type ‘gem install pry’ in your console. Once this is completed, be sure to put “require ‘pry’” at the beginning of any files or in any app where you will be looking to pry into your code. To tell the gem where you would like to execute, enter ‘binding.pry’ as a line of code. Once that has been completed, there are a few different ways to actually “hit the pry”. If you are working with test-driven development, you can run rspec. Once run, this will stop at the point in your code where you have entered ‘binding.pry’ and allow you to operate in the console. Another way is to run your entire program until it reaches the point in which you have your ‘binding.pry’. The code you are testing can often dictate where the best place for the pry is, as well as the best way to hit it.

Positioning is Key

Placement of ‘binding.pry’ in your code can completely change the scope of what it can do. Early on in my coding career, one of the most helpful uses was when enumerating through data. A well placed binding.pry provides clarity on what each object is acting upon.

You can position your binding.pry within your curly braces to see the values inside the pipes.

Test Within Your Pry Session

Alright, so you have finally found the correct value! If you used pry to get here, don’t leave just yet.

Using pry to chain calls and test data

In the example here, ‘@scores’ refers to an array of objects. Since the score is related to other objects, we have access to other calls such as “.first”. In this example we also dig a bit deeper and test other calls to ensure they will work before writing any code.

Leave Yourself Notes

Early on while you are still getting comfortable, leave yourself commented out notes about what you found while digging around your pry session. I found this to be a very helpful tool to dial in my own understanding.

‘Exit’ vs ‘Exit!’

Often times you may be using pry during some sort of iteration. If you need to check in on a value after the next iteration, simply use ‘exit’. This will move one loop in and keep you inside your pry session. If you have located the information you need, ‘exit!’ will completely back out of your pry session and take you back to your console.

If you are reading this and just embarking on your coding journey, know that as far as I can tell, it doesn’t get any easier. However, I have found that when you have a comfortable understanding of the tools at your disposal, it sure does get a lot more fun. Thanks for reading, good luck hittin’ the pry!

--

--