Back to Blog

Get in Touch

Pry Productivity From Your Code

By Jerry Cheung February 29, 2012 in ruby, sponsor, pry, support

Missing

You've heard of Pry right? It's a full-featured alternative to the classic IRB shell that we use in Ruby, and it's awesomesauce. If you've ever felt like you wanted a crowbar to pry open your code during runtime... well, Pry is your answer.

Pry is essentially a REPL (read–eval–print loop) tool that you can use to examine and debug your code. One of the best features is that local variables are available to Pry, saving you from recreating them as you normally would in an IRB session.

Installing Pry

I like to install pry into the global gemset since it's a tool even when I'm outside of a Rails project

rvm use @global
gem install pry

Replacing IRB with Pry

In your application initialization, add the following to replace IRB with pry by default. For example, Rails would add this code to config/initializers/pry.rb

begin
  require "pry"
  IRB = pry
rescue
  # do nothing if pry fails to load
end

Replacing ruby-debug with Pry

Between different versions of Ruby, installing and requiring ruby-debug can lead to annoying problems. 1.8.7 uses ruby-debug, 1.9.2 requires ruby-debug19, and 1.9.3 blows up when you try to use ruby-debug19. ruby-debug also depends on the linecache gem, which sometimes requires extra work to use with rvm and sometimes fails in environments when the native extensions fail to build.

Instead, skip all that headache with Pry! Anywhere you would use a 'debugger' statement, just call:

binding.pry

'binding' is a reference to the current local context. Enter 'exit' when you're finished with debugging, and the code will resume executing

Additional features

Pry has a ton of other productivity boosters built in. You can drop into a shell temporarily, browse docs without leaving your shell, edit and reload code, and send code snippets up to gist. For a full listing, check out their README

There's a ton of documentation for Pry and a growing community around it; if you're interested in jumping in be sure to start at their Github page for links to tutorials, screencasts, FAQs and a Wiki!

Disclaimer: We love Pry so much we were compelled to donate funds to the project. And write this blog post about it. If you like it, you can support them too!

Medium

Jerry Cheung

Jerry loves creating software. He started experimenting with Ruby on Rails in 2007 and got hooked on it after creating several personal projects. Upon graduating from Berkeley, he joined Coupa and later went on to start his own company Outspokes with several friends from Berkley. When he's not furiously typing, Jerry might be out running, brewing beer, or enjoying a BBQ and getting a serious sunburn.

More posts by Jerry Cheung

Jerry Cheung

Rails views are typically rendered after some controller action is executed. ...

Jerry Cheung

Twitter popularized the term "firehose API", to mean a realtime stream of dat...

Jerry Cheung

Rails ActiveRecord models have a lifecycle that developers are allowed to hoo...