20-Nov-2013 by Allison McMillan

Read Time: Approx. 2 minutes

The Pry talk!

I was really excited for this talk. In my local user group, Arlington Ruby, someone discovered Pry a little while ago and so now we all use it and are a little obsessed with what it can do. My guide, Conrad, gave the talk and is one of the main men working on Pry so I was super excited to see what it could do. Right now, I use binding.pry a bunch and kinda understand what it does but this talk really helped and also showed me a ton of other stuff that can be done with Pry.

TL;DR: Pry is really cool and is very powerful with a ton of built in shortcuts for effective debugging and working with your code.

So, let’s start with what is Pry? Pry catches exceptions. It is a REPL which means read, evaluate, print, loop. It is more robust, fun and easy to use, and adds introspection to the REPL. Introspection basically means that you are able to look around and see what your code is doing. This is what I’ve used pry for in the past. You can put binding.pry into your code and run it in your terminal and your terminal will automatically open pry instead of IRB and will allow you to both step through your code or look at each step in your code individually allowing you to see what’s going on and help determine and solve problems more effectively.

Conrad talked about recommending REPL driven development. So, instead of working out the code and testing it, he suggests you work out the code you’re going to use in a REPL first and then write the tests and the code it.

Now, for the cool tips and tricks he mentioned (note: I haven’t had a chance to try these all out since I’ve been back)…
?$mongo shows the documentation for a method
$$mongo shows the source code
ls Moped::Query shows the methods available

For more fun stuff, you can install the pry-plus gem and then require ‘pry-rescue/rspec’
Typing play-l6 will prompt line 6 to play that 1 line
Edit –m edits the current method (so if you want to fix an issue)
And then try-again will rerun that 1 test so that you don’t have to deal with rerunning the entire test suite just to see if you fixed that one test.

You can also use pry to vertically in a method by using the up and down keys. You can use the cd command to move through the code horizontally.

Typing an underscore will refer to the last line and typing __ or two underscores will give you two lines back.
ex raises the most recent exception
wtf? Gives you the first five lines of the stack trace so that you don’t have to scroll through a bunch of unnecessary stuff.
Finally whereami will show you where you are in the code!

This talk was really cool and super useful (as in, I can start using these shortcuts and tips immediately). It was a lot of fun to hear all about pry and all the things it can do.

Ready to chat?

Join my mailing list

* indicates required

Set up a free call

phone icon