What is Pry?
Pry is an awesome IRB replacement that offers a ton of powerful features. This post will highlight five of the most useful, so you can get more out of Pry.
To install Pry, type the following into your terminal:
$ gem install pry
To start Pry from the terminal, simply enter in Pry like so:
$ pry  pry(main)>
Once in Pry, you can start interacting with Ruby as you would with irb, but with other added features.
1. Use Shell Commands while in Pry
When inside a Pry session, it’s possible to execute normal shell commands,
ls. You can even run more advanced commands like
bundle or even run rails commands like
rails generate or
To do so, simply prepend a
. to the start of the shell command you would
like to run. Here’s some examples:
 pry(main)> .pwd /Users/Username  pry(main)> .ls Desktop Development Dropbox Movies Pictures Development Downloads Library Music  pry(main)> .cd Development  pry(main)> .pwd /Users/Username/Development  pry(main)> .ls ruby_rocks.rb Folder
2. Make Full Use of Pry’s Methods
Pry has a bunch of different methods that can really help you understand your code. Here are a few of the best:
cd lets you move into a new context, such as an object or scope. Here’s
how it looks in action
 pry(main)> self main  pry(main)> class Dog  pry(main)* def bark  pry(main)* puts "woof"  pry(main)* end  pry(main)* end nil  pry(main)> cd Dog  pry(Dog):1> self Dog < Object
In the above example, we are now inside the Dog class object. We can perform
other actions while inside an object, such as
ls allows you to list all the various methods that an object can perform.
We can see how it works by continuing our example
 pry(Dog):1> ls Object.methods: yaml_tag Dog#methods: bark locals: _ __ _dir_ _ex_ _file_ _in_ _out_ _pry_
As we can see, typing
ls shows us what methods an object has. In
this example, we see that Dog has the
show-method lets us see the original code of a method. This can be
of great value when trying to understand what it is a method does.
Continuing with our example, we can see the original bark method.
 pry(Dog):1> show-method bark From: (pry) @ line 3: Owner: Dog Visibility: public Number of lines: 3 def bark puts "woof" end
nesting can be used to help orient yourself in understanding the
scope and inheritance of the current object. Here’s how
works in our ongoing example.
 pry(Dog):1> nesting Nesting status: -- 0 main (Pry top level) 1 Dog
helpcan list out all the other methods that Pry offers that won’t be covered in this blog post. Try it out within Pry yourself to see all the different options.
3. Dive Into Running Code with binding.pry
binding.pry can be used to start a Pry session at any point in your
application. This can be great to use to really dive into code and
understand scope, methods and any sort of errors. Here’s how it works.
Let’s say we have a file, pry-example.rb, with the following code:
require 'pry' class Example def how_binding_works binding.pry end end demonstration = Example.new demonstration.how_binding_works
We then run the file within the terminal:
$ ruby pry-example.rb
After running the file, we are then put into the code at the point
binding.pry is located.
From: /Users/Username/pry-example.rb @ line 6 Example#how_binding_works: 5: def how_binding_works => 6: binding.pry 7: end  pry(#<Example>)>
We can then execute whatever commands we want in order to understand this place in the code.
 pry(#<Example>)> ls Example#methods: how_binding_works locals: _ __ _dir_ _ex_ _file_ _in_ _out_ _pry_  pry(#<Example>)> self #<Example:0x007fde422f8fc8>
4. Use Pry with Rails
It’s possible to use Pry with your rails app instead of the default rails
console. There are a few options for how to do so. One of the simplest is to
use the pry-rails gem with your
application. Doing so will cause
rails console to load Pry instead of IRB,
yet will still retain all of your rails informatin, such as your models,
and all the information in your database.
To set up your rails app to use Pry, simply add the following code to your gemfile:
gem 'pry-rails', :group => :development
bundle install and the next time you invoke the
you’ll be brought into Pry.
Pry-rails also comes with two additional methods that extend the functionality of Pry with your rails app. They are:
show-routes- This shows all of your routes for your application in the same way that typing
rake routesin the shell would do.
show-models- This lists all of the models along with all of their column name. It is the same as if viewing the schema.rb of your application. However, in addition, it will also show any associations your models have, such as
5. Explore All of Pry’s Awesome Documentation & Resources
Pry has some of the best documentation and resources around. Here are some of the best places to start with to get a deeper understanding of Pry.
Pry’s Main Site gives a great overview of the project.
The Pry Intoductory Screencast is easy to follow and does a wonderful job of demonstrating the power of Pry.
The RailsCasts episode, Pry with Rails, covers some great ways to use Pry with Rails.