Pry is an alternative to IRB and like IRB it provides a prompt for executing Ruby code. What makes it different is that it comes with many extra features. In this episode we’ll show you how Pry works and how to integrate it into your Rails applications.

Pry comes as a gem and is easy to install. We’ll install the pry-doc gem along with it and you’ll see more about this later.

terminal

$ gem install pry pry-doc

As we’re using RVM gemsets we’ll install Pry so that it’s available globally across all gemsets. We can do this with the following two commands.

terminal

$ rvm gemset use global
$ gem install pry pry-doc

Once we have Pry installed we can run it with the pry command and execute Ruby code just as we would with irb.

terminal

$ pry
pry(main)> 1 + 2
=> 3

Pry is much more than a simple calculator but before we go any further with it we’ll look at setting it up to work with a Rails application. The application we’ll use is the familiar blogging application we’ve used in several previous episodes.

If we run rails c from the application’s directory IRB will start up. To use Pry instead all we need to do is run pry and pass in the Rails environment file. When we do we’ll have access to our application’s models just as we would in the standard Rails console.

terminal

$ pry -r ./config/environment
pry(main)> Article.count
=> 3

Now that we have Pry set up we can take a look at some of its features. If we type help we’ll get a list of all of the commands that Pry supports. The two we’ll use most often are cd and ls so let’s look at those. The cd command changes the current scope, so if we type cd Article we’ll move into the Article class and we can check the current scope at any time by running self.

This command returns the list of objects that we’ve gone in to. If we type exit this will pop us out back into the previous object, in this case the first article. If we exit again we’ll be taken back into the Article class.

The other most-commonly used command is ls and this lists variables and methods. By default it lists all of the variables in the current scope, so if we’re currently in the Article class we’ll see its methods when we run it.

Some Pry commands support flags and we can get a list of a command’s flags by running the command with the -h option. If we run ls -h we’ll see all of the options it supports, including -m which we can use to display a list of methods in the class and -M which returns a list of the instance methods. We can also pass in any object or class to get a list of methods for that rather than for the current scope.

Another useful Pry command is show-doc. Let’s say that we want to know to the Array class’s in_groups_of method works. We can find out by running show-doc Array#in_groups_of.

We can also call show-doc directly on objects. Our scope is currently the Article class so we can call all to return an array of the articles. We can run show-doc all.in_groups_of to return the same documentation as above.

Another useful command is show-method. This shows us the source code of any method. We can use this to see the source of in_groups_of. (It’s worth pointing out here that when we’re entering commands Pry has autocompletion that we can activate by pressing the TAB key.)

Similarly we have an edit-method command. When we run this against a method it will open up the relevant source code file in a text editor and take us to the appropriate line.

We can also run shell commands by prefixing them with a full stop. If we run .ls in Pry this will run the ls shell command and list all of the files in the current directory.

Pry is also useful for debugging. In our Article model we have a word_count method that should return the number of words in an article’s content. There’s a bug in the method as it stands, however, so that it always returns 0, no matter what the content in the article. We can look at the method by cd-ing to the first Article and then running edit-method word_count. This is what the method looks like:

We can add a breakpoint to the code any point by calling binding.pry. If we add that immediately before the words.size line above and save the file then when we call the word_count method again it will stop running at binding.pry and we’ll return to the Pry prompt.

We have access to all of the method’s local variables here so we can call words to see the contents of the words array.

terminal

pry(#<Article:0x1008c3f38>):3> words
=> []

The array is empty so it appears that there’s something wrong with the regular expression that scans the content. If we look at it we’ll see that there are two backslashes there where there should be one. To fix this we can run edit-method word_count again, fix the regular expression, remove the binding.pry line and save the file.

We can test our fix by calling word_count again and this time it works as expected.

terminal

pry(#<Article:0x1008c3f38>):3> word_count
=> 55

Sometimes we’ll want to debug something in the controller or view layers of our application and not necessarily through the console. Pry can help with this too. First we’ll need to add a reference to Pry in the Gemfile.

When can now run bundle to install the gems then rails s to start up the server. We can now add calls to binding.pry anywhere in our controllers to add a breakpoint.

/app/controllers/articles_controller.rb

defindex@articles = Article.all
binding.pry
end

If we go to that page in a browser now it will “stick” while loading but inside the terminal we’ll have a Pry prompt that has stopped at that breakpoint. As we could in the model we can inspect the values of any local or instance variables. Once we’ve finished we can type exit-all to allow the request to complete.

That’s it for this episode on Pry. It’s a really useful gem and does much more than we’ve covered here. The wiki covers more than we’ve done here and includes a link to a very useful screencast by Joshua Cheek.