arrow-flex-shortarrow-flexarrow arrowsbinocularschartcogsflex-icongeargithubglobelightnings linkedinlock plantscalestarget triangle-icon twitter

Enter Pry

May 20, 2019

When to debug?

More often than not, we run into unexpected error messages during software development. When this happens, we usually start by reading the console messages in order to identify the exact point where the application is crashing.

We can also try printing logs at different points within the code, but this could be a bit inefficient and become an eyesore of sorts. So, what would be our next step? You’ve probably guessed it: debugging to the rescue!

What is Pry?

Pry REPL is an alternative to the interactive Ruby shell (aka. irb) and it allows you to jump into a point in your code, run commands, edit them and see exactly what your code is doing. REPL stands for read-eval-print and loop. Pry works as a run-time developer console and debugger for the Ruby language.

Install Pry and run it:

$ gem install pry
$ pry
[1] pry(main)>


Pry is highly configurable and allows us to put customizations on the ~/.pryrc file located in the home directory. For example, we can define aliases to get the last 20 commands in our history or even define dummy and experimental methods, that we can later use as a sort of monkey patching acting only inside our Pry session, as shown below:

#!/usr/bin/env ruby
Pry.config.commands.alias_command 'h', 'hist -T 20', desc: 'Last 20 commands'

# Add toy method to array
class Array
  def self.toy(num = 10, &block)
    block_given? ?, &block) : { |i| i + 1 }

Now, let’s try it out:

Load the ‘hello_world’ gem

[1] pry(main)> require 'hello_world'
=> true

move into the HelloWorld object

[2] pry(main)> cd HelloWorld

and list the available methods

[3] pry(HelloWorld):1> ls -m
HelloWorld.methods: say_hello

[4] pry(HelloWorld):1> say_hello
hello world
=> nil

If we want to avoid getting the returned value, such as nil, we can simply add a “;” at the end of the line.

[5] pry(HelloWorld):1> say_hello;
hello world

Next, let’s check out the say_hello method definition

[6] pry(HelloWorld):1> show-method say_hello

From: /var/lib/gems/2.5.0/gems/hello_world-0.0.2/lib/hello_world.rb @ line 2:
Owner: #<Class:HelloWorld>
Visibility: public
Number of lines: 3

def self.say_hello
  puts 'hello world'

As you can see, it provides information about the location of the method (within the gem file).

We can keep moving around and check our nesting status as well as jumping around back to a specific level.

[7] pry(HelloWorld):1> cd say_hello
hello world

[8] pry(nil):2> nesting
Nesting status:
0. main (Pry top level)
1. HelloWorld
2. nil

[9] pry(nil):2> jump-to 1

Whenever you feel lost, just ask Pry where you are ;)

[10] pry(HelloWorld):1> whereami
Inside HelloWorld.

And of course, Pry can be invoked in the middle of your application for debugging. It simply opens a Pry session at the point it is called and makes all program states available at that point. Simply start a session by entering the binding.pry magic line and voilà!

Just keep prying around and discovering additional features as you go along. Please let us know how it goes.

[11] pry(HelloWorld):1> exit
=> HelloWorld

[12] pry(main)> exit

$ See you later!



By using this website you agree to our cookie policy