What is binding.pry?

Romina Velarde
2 min readNov 15, 2020

--

‘Pry’ is an essential tool for debugging. I will say that this is my salvation when writing very complex code. Why? Because it can access your code and enables you to test and see what your code is doing, so you can instantly identify errors.

How do I use Pry?

If programming with Ruby, you will have to invoke it by typing require ‘pry’ at the top of your file in line number 1. Alternatively you can create a Gemfile

and set this inside:

source “https://rubygems.org"

gem ‘pry’

gem ‘require_all’

then run bundle install on your terminal. Now you can set your binding.pry!

Where to set binding.pry?

‘Pry’ can be really tricky to hit. Where to set it is very important. If you’re writing a block with if else keep in mind that it will not hit it if nothing is written below it, neither test non logical code. For example:

it will freeze your code where you run it:

Now, we want to test what’s above binding.pry inside rides method. Let’s test ride.

works like magic!

When you're creating a “one liner code”, the binding.pry will not hit if it’s outside the brackets. Set it like this:

You’re all set to debug!

--

--