An Introduction To Pry, A Ruby REPL

I'm a Ruby, Clojure, and now Node.js developer. When I was doing Clojure, I had a REPL driven development style.

Those of you that don't know what a REPL is, it's a read-eval-print-loop. You've probably used a REPL before and didn't even know it. The interactive ruby shell, otherwise known as IRB is the default REPL that ships with Ruby. It's also the same REPL you get when you run rails console.

The difference between developing in a Lisp like Clojure and a language like Ruby is how integrated the REPL is to how you develop. In Clojure, you get to play with code and mold it.

People that haven't done this style of development before really should experience it. It'll change the way you'll think about programming and your workflow.

When I started doing some Ruby and Rails projects again, I missed having my REPL. Developing with the rails console opened in my terminal and writing methods line by line didn't cut it.

Enter Pry.

Pry also aims to be more than an IRB replacement; it is an attempt to bring REPL driven programming to the Ruby language. It is currently not as powerful as tools like SLIME for lisp, but that is the general direction Pry is heading.

I first heard about Pry during a job interview at Braintree. I've since checked it out and have have integrated it into my workflow.

I'll start with a simple tutorial to just Pry and since I and many of you are likely to be web developers, how to integrate it with Rails.


The first thing you want to do is install pry by simply running gem install pry.

Pry has many different commands, which are similarly named to the command line utilities you're already using, like cd and ls.

You can open up a Ruby project you've been working on or open up your favorite editor and type this in:

{% highlight ruby %}

intro.rb

require ‘pry’

puts “Start”

Start a pry shell

binding.pry

puts “End”

<p>Run the script and you'll see start, and then the program will pause and you can play with it while it's running. Basically, the program pauses exactly where you put the binding.pry line so you can do some debugging if necessary.</p>
<p>You can use the shell as if you were to use irb. Enter in 1 + 1 and you'll get 2 and so forth.</p>
<p>And to exit, simply type exit.</p>
<p>Now here's the cool part. If you go back to the file and edit it to add in functions, you can hot reload the code.</p>
<p>So, if I were to go and add the function add like so:</p>
{% highlight ruby %}
require 'pry'

puts "Start"

def add(a, b)
  a + b
end

# Start a pry shell
binding.pry

puts "End"

You can go back to your repl and type in add(1,2). You'll see that you'll have an undefined method error. That's because we haven't reloaded the file yet.

Type in reload-code in pry and then you'll see the file is reloaded and the method add shows up.

Now if you do add(1,2), you can see the return value, which is of course 3.

It gets better when you add in classes. Let's do that:

{% highlight ruby %} require 'pry'

puts “Start”

def add(a, b) a + b end

class Car attr_accessor :make, :model def initialize(make, model) @make = make @model = model end

def honk puts “Beep” end end

Start a pry shell

binding.pry

puts “End”

<p>Reload the code again in pry.</p>
<p>Again, you can do the same things you can do with irb.</p>
{% highlight ruby %}
car = Car.new("Tesla", "Model S")
car.honk

A really cool thing you can do is list all the methods of an object.

Try it:

{% highlight ruby %} ls Car ```

Since we're listing the methods of the class, we don't get to see the instance variables. If you were to instead do ls car, using the instance of car we created, you would also get its instance variables outputted.

For help and a list of all commands, type help.

Pry on Rails

Pry Rails is a gem that adds in Rails specific features.

Open up the Gemfile of one of your Rails projects and add this line:

gem 'pry-rails', :group => :development

Now your default rails console has been replaced with pry instead of irb.

If you type in help, you'll see a list of Rails specific commands:

{% highlight ruby %}

Rails recognize-path See which route matches a url. show-middleware Show all middleware (that rails knows about). show-model Show the given model. show-models Show all models. show-routes Show all routes in match order.


<p>These commands are significantly faster than running the rake task because the Rails environment is already loaded for you.</p>
<p>Now you can go into your controller and add in the binding.pry line to start pry and it'll pause the execution of the request at that line and open up the pry shell where you ran rails s. You can now play and debug your application without having to use puts everywhere.</p>

Join the 80/20 DevOps Newsletter

If you're an engineering leader or developer, you should subscribe to my 80/20 DevOps Newsletter. Give me 1 minute of your day, and I'll teach you essential DevOps skills. I cover topics like Kubernetes, AWS, Infrastructure as Code, and more.

Not sure yet? Check out the archive.

Unsubscribe at any time.