Understanding Your Ruby Code Better With ‘pry’

Ashab Ahmed
The Startup
Published in
4 min readSep 11, 2020

As you continue your journey as a beginner “rubyist” you will notice an increase in complexity in the coding challenges or “labs” you are approached with. That being said, in these early stages you’re probably just “printing” or “putsing” your code for testing or inspection purposes. This may be helpful and can work for now, however when the data gets more intricate those methods will lose effectiveness. Maybe, you’re just relying on the Test Driven Development (TDD) setup using the test results to help you understand your code and help you pass. That shouldn’t be the primary goal of your coding mindset nor is it practical down the road, as I’ve learned. So what’s the solution? One of them is getting yourself familiar with implementing binding.pry.

What is ‘pry’?

Pry is a REPL (Read, Evaluate, Print, Loop), which is an environment that you can use to interact with your code. Similar to the IRB you may already be prompted to utilize. Overall, Pry is different than the IRB besides interacting aspect. It’s much better! Firstly, pry provides syntax highlighting.

IRB text in the terminal.
IRB text in the terminal.

Above is an image of how interacting with the IRB can be very bland and everything looks the same. This makes it hard to differentiate between code and syntax errors you may trying to find.

Here you can see the binding.pry syntax highlighting. Where different data types and objects are colored making it easier to find what you are looking for. Secondly, pry freezes the program wherever you added ‘binding.pry’ (line 24 in above screenshot) and you can use the terminal to test your variables, classes, objects, and etc. Understanding return values is crucial to further your development in learning to code. I’v been told many times by many experienced programmers that you should never be guessing what your return value will be. However, that’s exactly what I was doing to pass all my labs before getting into the really complicated stuff and before using pry.

Using binding.pry to understand code better

This above screenshot shows how crazy these nested data structures can be. You will be approaching these labs or data structures and won’t be able to tackle them with the previous methods before. You would definitely need a way to access the data and interact with it to understand what each key and value return which input.

An additional reason why pry is effective is because when something breaks in your code and you have errors and you can’t see what the reason is, you can use binding.pry to check. You can place your pry within your methods so you can fully grasp where that error is occurring to help you n fix that error and prevent future errors AND save you hours of guess work.

Seriously, utilize binding.pry!

Thus, my continuous advice to you is learn how and when to use ‘pry’, not only for debugging but for getting a clear understanding for the code that’s being presented to you AND your own methods as well. You will use ‘pry’ throughout your time for future labs and the bootcamp. Even when you move forward to learning Ruby in different frameworks like Rails. Hope this article helps you to not dismiss the value of ‘binding.pry’. Here you can retrieve another article that explains on how to effectively use ‘pry’ with helpful tips and tricks. Additionally, here you can find more advanced binding.pry commands and tips for your future endeavors!

--

--