PRY, your new best friend!

Second week of Web Development Bootcamp at Flatiron School and Pry has already become essential! When I first discovered Pry I did not really understand how it worked. To be honest, I still don’t always understand it, but from what I do understand it is a powerful and very helpful tool.

Pry is a REPL (Read Eval Print Loop), meaning you type in some expression in your console and the result is returned immediately. Yes, IRB is also a REPL but Pry does way more — i.e. you can inject it directly into your program. So, let’s talk about it!

First things first, pry is a Ruby gem so in order to use it the first step is:

(pry-doc provides MRI documentation and source code. We will see later how to use it.)

Once installed, pry can be called directly from your terminal:

1. Pry to break into some code

When called directly from your terminal, Pry can be used to try out some methods, display documentation or navigate object state by using shell navigation commands. By typing help in your Pry command, you will see a list of all the available functionalities:

The list continues…

By typing the name of a command with -h, you will get all the help options for that specific command:

The same way you would use cd and ls in your bash to navigate through your files and directories, you can use cd and ls in Pry to navigate around Pry and change scope.

You can cd into an object:

and start playing with it:

You can then use ls to have a list of all the methods available for that specific object:

And if you don’t remember exactly how a method works, you can use the show-doc and show-method commands to refresh your memory (this is when the gem pry-doc is useful!).

show-doc will give you the method’s documentation:

show-method will pull the source for the method and will display it with syntax highlighting:

And as we are all lazy programmers, here are the shortcuts: ? for show-doc and $ for show-method.

2. Pry as a runtime developer console and debugger

Pry can be invoked while running your program by injecting it right where you have an issue. It will help you check what is wrong, what your method is returning. In order to do so, you need to require ‘pry’ in your file and use binding.pry where you want the breaking point to happen.

require ‘pry’ on the first line of your file

When running your program, it will break where you added binding.pry and will open a pry console where you will be able to play with your method and variables:

This is only the beginning of what you can do with Pry. By setting a default editor, you can use the editor integration of Pry, with the edit command. You can even create your own custom commands. Worth exploring, right? If you want to learn more about it, here are the main resource pages on github and http://pryrepl.org/

Here is a Cheat Sheet.

And a few useful shortcut/methods:

Happy coding! Happy prying!

Lili Ouaknin Felsen

Written by

Software Engineer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade