Debugging Ruby Code with Pry

Your comprehensive guide to Pry and how to use it .

Debugging: the process of identifying and removing errors from code

What is Pry?

Pry is a REPL (Read, Evaluate, Print, Loop) that pauses your code wherever inserted and allows you to test/debug. When your code gets to the ‘pry’ it will freeze and your terminal will turn into a REPL right in the middle of your program.

How do I use Pry?

The first step is to make sure you have the pry gem installed by typing the following into your terminal:

gem install pry
# test.rb
require 'pry'
require 'pry'

def my_fancy_method
inside_method = "We are now inside the method."
puts inside_method
pry_coming = "We are about to see how pry works!"
binding.pry frozen = "Pry froze the program before it got to this point!"
puts frozen
end

my_fancy_method
3: def my_fancy_method
4: inside_method = "We are now inside the method."
5
: puts inside_method
6: pry_coming = "We are about to see how pry works!"
=> 7: binding.pry8: frozen = "Pry froze the program before it got to this point!"
9
: puts frozen
10: end
[1] pry(main)>
[1] pry(main)> inside_method=> "We are now inside the method."[2] pry(main)>
[[1] pry(main)> inside_method=> "We are now inside the method."[2] pry(main)> frozenNameError: undefined local variable or method `frozen' for main:Object
[3] pry(main)> exitPry froze the program before it got to this point!

Using Pry to Debug

Ok. Now that we understand how to install, and use pry, we’ll see how it helps us debug code. Here are the most common uses of Pry for me so far in my early journey through the coding world.

Use Case #1: Checking Variables

One common mistake for early coders is improperly assigning values to variables. For example, let's say you had the following code:

require 'pry'

def simple_cubing_tool(number)
number * number * number
puts "The answer is #{number}!"
end

simple_cubing_tool(4)
// ♥ > ruby practice.rb

The answer is 4!
require 'pry'

def simple_cubing_tool(number)
number * number * number

binding.pry
puts "The answer is #{number}!"
end

simple_cubing_tool(4)
3: def simple_cubing_tool(number)
4: number * number * number
=> 5: binding.pry
6: puts "The answer is #{number}!"
7
: end
[1] pry(main)> number
=> 4
require 'pry'

def simple_cubing_tool(number)
num_cubed = number * number * number
puts "The answer is #{num_cubed}!"
end
// ♥ > ruby practice.rb

The answer is 64!

Use Case #2: Seeing where you are in a nested Hash/Array

Let’s say we have an array of hashes (AOH) defined as follows:

nested = [
{:fruit => {
:apple => 1,
:banana => 2,
:grape => 6
},
:pets => {
:fido => "dog",
:whiskers => "cat",
:charles => "mouse",
:bitey => "snake"
},
:teams => {
:new_york => {
:baseball => ["mets", "yankees"],
:basketball =>["knicks", "nets"],
:football => ["giants", "jets"],
:hockey => ["rangers", "islanders"]
},
:los_angeles => {
:baseball => ["dodgers", "angels"],
:basketball =>["lakers", "clippers"],
:football => ["rams", "chargers"],
:hockey => ["kings"]
},
:chicago => {
:baseball => ["cubs"],
:basketball => ["bulls"],
:football => ["bears"],
:hockey => ["blackhawks"]
}
}
}
]
def list_basketball_teams_by_city
nested.each do |element|
element.each do |outer_key, outer_value|
binding.pry
end
end
end

list_basketball_teams_by_city
// ♥ > ruby practice.rbTraceback (most recent call last):
5: from practice.rb:45:in `<main>'
4: from practice.rb:37:in `list_basketball_teams_by_city'
3: from practice.rb:37:in `each'
2: from practice.rb:38:in `block in list_basketball_teams_by_city'
1: from practice.rb:38:in `each'
practice.rb:39:in `block (2 levels) in list_basketball_teams_by_city': undefined method `pry' for #<Binding:0x00007faadd86ecc0> (NoMethodError)
require 'pry'def list_basketball_teams_by_city
nested.each do |element|
element.each do |outer_key, outer_value|
binding.pry
end
end
end

list_basketball_teams_by_city
// ♥ > ruby practice.rb
From: /Users/sean.laflam/Development/code/mod1/practice.rb:41 Object#list_basketball_teams_by_city:
38: def list_basketball_teams_by_city(array)
39: array.each do |element|
40: element.each do |outer_key, outer_value|
=> 41: binding.pry
42: end
43: end
44: end
[1] pry(main)>
[1] pry(main)> outer_key=> :fruit[2] pry(main)> outer_value=> {:apple=>1, :banana=>2, :grape=>6}
def list_basketball_teams_by_city(array)
array.each do |element|
element.each do |outer_key, outer_value|
if outer_key == :teams
outer_value.each do |city, sports_hash|
binding.pry
end
end
end
end
end
38: def list_basketball_teams_by_city(array)
39: array.each do |element|
40: element.each do |outer_key, outer_value|
41: if outer_key == :teams
42: outer_value.each do |city, sports_hash|
=> 43: binding.pry
44: end
45: end
46: end
47: end
48: end
[1] pry(main)> sports_hash=> {:baseball=>["mets", "yankees"],:basketball=>["knicks", "nets"],:football=>["giants", "jets"],:hockey=>["rangers", "islanders"]}[2] pry(main)> city=> :new_york
require 'pry'def list_basketball_teams_by_city(array)
array.each do |element|
element.each do |outer_key, outer_value|
if outer_key == :teams
outer_value.each do |city, sports_hash|
sports_hash.each do |sport, team_name_array|
if sport == :baseball
puts "The basketball teams for #{city} are #{sport}."
end
end
end
end
end
end
end
list_basketball_teams_by_city(nested)// ♥ > ruby practice.rbThe basketball teams for New York are baseball.The basketball teams for Los Angeles are baseball.The basketball teams for Chicago are baseball.
if sport == :baseball
binding.pry
puts "The basketball teams for #{city} are #{sport}."
end
38: def list_basketball_teams_by_city(array)
39: array.each do |element|
40: element.each do |outer_key, outer_value|
41: if outer_key == :teams
42: outer_value.each do |city, sports_hash|
43: sports_hash.each do |sport, team_name_array|
44: if sport == :baseball
=> 45: binding.pry
46: puts "The basketball teams for #{city} are #{sport
47
: end
48: end
49: end
50: end
51: end
52: end
53: end
[1] pry(main)> sport=> :baseball[2] pry(main)> team_name_array=> ["mets", "yankees"][3] pry(main)>
require 'pry'def list_basketball_teams_by_city(array)
array.each do |element|
element.each do |outer_key, outer_value|
if outer_key == :teams
outer_value.each do |city, sports_hash|
sports_hash.each do |sport, team_name_array|
if sport == :basketball
puts "The basketball teams for #{city} are #{team_name_array}."
end
end
end
end
end
end
end
list_basketball_teams_by_city(nested)
// ♥ > ruby practice.rb
The basketball teams for New York are ["knicks", "nets"].The basketball teams for Los Angeles are ["lakers", "clippers"].The basketball teams for Chicago are ["bulls"].~/.../code/mod1 // ♥ >

Use Case #3: Anywhere else you need to debug your Code!!!

You can use Pry in any application that you seem fit where you need to debug errors in your code. Now that you’re a Pry wizard get out there and test it on your own!

NYC based SWE

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store