Pry Me a River

Liam Hanafee-Areces
4 min readOct 1, 2020

--

How to debug with binding.pry and why you should.

If you aren’t using binding.pry in Ruby, you probably should be. Despite being clearly told how this helpful this REPL is repeatedly during my first week at Flatiron School, it took me until my second week to fully appreciate it. In retrospect, I think that it just seemed like a helpful, but non-essential tool for debugging. With the demanding pace of bootcamp, I felt like it would be a waste of time to learn something that I viewed as a luxury. Don’t think that way. I would have saved myself hours of grief by adopting pry even just a couple of days sooner.

Pry is a REPL that you can use in your terminal(much like IRB). While IRB is useful, it is rather annoying to use for debugging your methods. IRB doesn’t know what your methods look like unless you copy and paste them in. Pry, on the other hand, knows all of the code that has been run before hitting binding.pry. All you have to do is write “require ‘pry’ ” at the top of your file, and then write “binding.pry” wherever your code seems to be breaking. You won’t have to copy and paste a thing. Hopefully the example below will illustrate just how easy and helpful Pry is.

The class Bloodoath
The tools/console.rb file where I have created some test instances of all of these classes and placed my ‘binding.pry’

Pry is especially useful when building relationships between your classes. In the example above, the class ‘Bloodoath’ is initiating followers into cults. Cults have many followers through bloodoaths, and followers have many cults through bloodoath. A bloodoath belongs to an instance of cult and instance of follower, and must be initialized with one of each, as well as an initiation date. If the class is built correctly, each bloodoath will know it’s initiation date as well as the cult and follower that it is linking together. I placed a require ‘pry’ at the top of my console.rb file. Then, I created some instances of each class and put a binding.pry underneath. All I had to do after that was run ‘ruby tools/console.rb’ in the terminal to run the file and start my debugging session.

the ‘=>’ next to line 20 marks where pry begins.

I chose the instance of bloodoath named ‘oath’ to run some tests on. The first thing I wanted to check was whether ‘oath’ new what cult it belonged to, so I ran ‘oath.cult’. Sure enough, pry returned the entire instance of cult. You can even run ‘oath.cult.slogan’ and get just the slogan back. Even when your code seems to be ok, pry helps you check every single part and will really help you gain a better grasp on how all of your classes and methods interact. When you do get error messages, you can still play around with all of your methods and see how they are interacting. This will help you understand object relationships much more quickly, and overall help you interact with Ruby on a higher level.

What is binding.pry and how does it work?

Binding essentially takes a snapshot of everything that Ruby knows at the moment it is instantiated. Pry is a debugger Ruby gem that can be used without binding(for example, it is the built-in debugger in rake console). However, in your early forays into Ruby binding.pry is the most common way you will be using both of them. When you combine binding and pry the result is what you would expect with a loose understanding of what they do invidually: it takes a snapshot of everything that exists in the program up until the binding.pry is hit and lets you debug! No more copying methods and instantiating your classes in IRB! You can create an instance of your class or a variable, pry, exit, change your method, and pry again. Guess what? Your test instances are still there. This might not save you time if you only had to do it once, but you’re gonna have to do it thousands of times more than that as a developer.

There may be times where pry doesn’t seem to be working. It does bug out sometimes, but more often than not, there is something broken in your code before the pry, meaning that you never hit your pry. If this happens, throw some more prys in there! As you can see in the picture I posted above, pry shows you exactly what line of code it is up to. The thought of not having this wonderful tool at my disposal makes me wanna… pry.

If at first you don’t succeed, Pry, pry again.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response