stick the line:
binding.pry
and when execution reaches that line, it
will stop, dropping me into a Pry session.
This works just fine when using Webrick,
but it also can be configured to work with
Pow, a popular server system for OS X:
def show
binding.pry
end
I made the above modification to one
of the controllers on my site, and then
pointed my browser to a page on which
it would be invoked. It took a little bit of
time, but the server eventually gave way to
a Pry prompt. The prompt worked exactly
as I might have expected, but it showed
me the current line of execution within the
controller, letting me explore and debug
things on a live (development) server. I was
able to explore the state of variables at the
beginning of this controller action, which
was much better and more interactive than
my beloved logging statements.
Conclusion
Pry is an amazing replacement for the
default IRB, as well as for the Rails console.
There still are some annoyances, such as its
relative slowness (at least, in my experience)
and the fact that readline doesn’t always
work perfectly with my terminal-window
configuration. And as often happens, the
34 / JULY 2012 / WWW.LINUXJOURNAL.COM
existence of a plugin infrastructure has led
to a large collection of third-party plugins
that handle a wide variety of tasks.
Reuven M. Lerner is a longtime Web developer, consultant
and trainer. He is also finishing a PhD in learning sciences at
Northwestern University. His latest project, SaveMy WebApp.com,
went live this spring. Reuven lives with his wife and children in
Modi’in, Israel. You can reach him at reuven@lerner.co.il.
Resources
The home page for Pry is https://github.com/
pry/pry. You can download the source for Pry
from Git, or (as mentioned above) just install the
Ruby gem. The Pry home page includes a GitHub
Wiki with a wealth of information and FAQs about
Pry, its installation, configuration and usage.
A nice blog post introducing Pry is at
http://www.philaquilina.com/2012/05/17/
tossing-out-irb-for-pry.
Finally, a Railscast about using Pry, both with
and without Rails, is at http://railscasts.com/
episodes/280-pry-with-rails.
I also mentioned iPython at the beginning of
this column. Pry and iPython are very similar
in a number of ways, although iPython is
more mature and has a larger following. If you
work with Python, you owe it to yourself to try
iPython at http://ipython.org.