Tossing Out IRB for Pry

Tossing Out IRB for Pry

This week I spent some time learning how to use Pry. For those of you who haven’t tried this wonderful alternative to IRB, spend two hours with it. You’ll never go back. I won’t be hitting on all the features Pry has to offer (because there are plenty), just the ones that immediately jumped out as useful to me. Pry has some of the best documentation and one of the friendliest, most helpful IRC channels out there so if you want more in-depth coverage, go check them out.

Without further ado,

$ gem install pry
$ pry

REPL Done Right

I can’t tell you how many times I’ve started to write out a quick hit method in IRB, only to accidentally typo something and then hit enter. Frustration at these moments knows no bounds. But Pry offers a couple nifty solutions. (Note that your prompt may look a little different since I’ve added Pry.config.prompt = Pry::NAV_PROMPT to my pryrc file to give me a little more information.)

There’s a few pretty cool things going on here so let’s break it down.

  • First notice the [1] on the left side. This is called the expression number. Simply put, it keeps track of what expression you’re on and it won’t change until you’ve completed the current expression, which in our case, is when I finish the method definition with an end statement.

  • The show-input command repeats back to us the contents of the current multi-line expression. This is helpful for figuring out the next part of our little fix because it also outputs line numbers. Note that line numbers and expression numbers are two different things in Pry.

  • We want to fix our error in line 2 of our multi-line expression. We do that with the amend-line command. amend-line accepts the line number (or a range of line numbers in N..M format) and the replacement code. If we need to we can pass in ! as the replacement code to delete the line(s) entirely.

  • Then we can end our expression or continue adding more code. If I wanted to suppress the output value of the expression, I’d end the code with end;. Since I didn’t do this, our expression outputs nil.

And just like that, we fixed up our bugged method.

A very neat alternative to this is the edit command.

edit inside your multi-line expression will let you finish your expression in the comforts of your editor. To customize what the default editor is, you have to add the line Pry.config.editor = "your_editor" to your pryrc file. So for instance, I have Pry.config.editor = "vi" in there, but yours might be "mate".

Outside of the expression and without a switch, edit throws us into our editor again but with the last input buffer as the contents of the file. Want to start with an empty file instead? Try edit -t. Like most commands in Pry, the edit command goes deeper still. To learn more, check out the local help by typing help edit or edit -h. I could do this with every command I’ve learned so far and has been unbelievably useful in learning Pry.

What if you want to edit a method that you worked on at an earlier stage in your Pry session? The edit-method command will help you out there.

It works nearly the same as edit but accepts a method as its argument. An added cool factor is it actually accepts methods in ri format, so if you’re outside the scope of the method’s class, you can scope in using something like this edit-method Pry#run_command.

Scoped

Now we get to some very interesting features. Pry offers you the ability to “cd” into different scopes. Once we’re in there we can do all sorts of stuff.

Pry also offers a recursive search command called find-method. You can pass in a method or a partial method name as its argument and it will list all the methods that match. Cooler still, pass in the -c switch to search for source code within the method.

Additional Tools for the Job

There are a few additional plugins you can add to Pry’s repertoire of usefulness. I’ve only started to scratch the surface on these ones but they’re worth mentioning for you to check out yourself.

Gist

After you’ve installed the gist gem, you have access to the gist command inside of Pry. As the name might imply, this allows you to gist code up to your github account.

pry-nav and binding.pry

Right out of the box, Pry can be invoked during runtime by putting binding.pry in your code. This session then inherits the local environment at the moment of invocation, allowing you to poke and prod at the current state of your code. In other words, you can use Pry as a lightweight debugger. (Note: this requires a require 'pry' statement at the top of the relevant file.

require 'pry'

class DebugThisShiz
  def creepy_critters_in_your_code
    mixed_array = [ "dung beetle", "praying mantis", 5 ]
    binding.pry       # <- Pry session will be invoked here
    mixed_array.inject { |memo, obj| memo + obj }
  end
end

To add even more punch, you can install the pry-nav gem and make available the next, step, and continue commands so you can move through your code with even greater dexterity.

Rails and Pry

Pry now offers an integrated Rails plugin that defaults rails console to Pry instead of IRB, while still offering the full Rails environment. For Rails 3 and up, just add this line to the gemfile:

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

After a bundle install, you’re good to go. Without the plugin, the process is a slightly more difficult (slightly). Check out this page for more info.

ri

You can use ri inside of Pry just like you can from the command line for all your offline documentation goodness.

help

I briefly mentioned using help command_name above, and I want to retirate how useful this is for learning what’s at your disposal. For a list of all the different commands and a one liner description, try just help.

Conclusion

I’ve been using Pry for a few months now but only on a very topical level. Spending just a few hours this week plumbing the depths of Pry has brought my effectivity in the REPL, and I’d even argue Ruby, up quite a bit. I love every bit of this tool and the maintainers do an excellent job reaching out and listening to the community. Within minutes of first joining the IRC channel, John Mair (aka banisterfiend) welcomed me with a warm hello and asked me if I had any questions. Within a few hours, John had me create my first github issue for a feature request. A huge thank you to him and his fellow maintainers for this project and all the questions I had for those kind folks. Thanks!

Resources

Pry Wiki
Josh Cheek’s Screencast
Jonathan Jackson’s Give it a Pry
The Pry IRC Channel #pry hosted on Freenode