Pry is an amazing tool that can supercharge the software development process. Pry is a REPL (Read, Evaluate, Print, and Loop) like rpl.it and IRB. However, Pry has additional features that make it especially helpful for writing and debugging code.
You may already be familiar with the
binding.pry method. If not, check out this Tutorial by Benjamin Tan Wei Hoa.
Pry's awesome features don't stop there though. You can also navigate through files, lookup relevant methods and documentation, and even trace the stack to hunt down a bug.
Getting Started - Navigating State with
Pry helps you learn how to use advanced features. Here are some tips for accessing these features.
To get started, it helps to know Pry’s
ls. Pry’s cd and ls methods are similar to the terminal commands with the same name. They both help users navigate around files and display directory contents. Type “whereami” in a Pry session to check where you are.
On to the code - open up a pry session and cd into Pry itself:
// ♥ Pry  pry(main)> whereami At the top level.  pry(main)> cd Pry  pry(Pry):1> whereami Inside Pry.  pry(Pry):1>
As you can see from the code above, you are now “inside Pry.” Pry is a program like any other and contains numerous files and Commands. Now,enter
help to get a display of all sorts of Pry commands. Enter
exit! to back out of your current Pry session.
Pry’s Introspection methods like
show-code help you learn background information about the program/class/method that you’re working with.
Show Documentation - ?
The Pry command
? for short) does exactly what it says: it shows you any relevant documentation for the file you’re working with. This saves you from having to hunt down the docs online. For example, typing ? in Pry will bring up Pry’s docs:
// ♥ Pry  pry(main)> cd Pry  pry(Pry):1> ? From: /Users/edwardezekiel/.rvm/gems/ruby-2.3.3/gems/pry-0.11.3/lib/pry/pry_instance.rb @ line 2: Class name: Pry Number of monkeypatches: 6\. Use the `-a` option to display all available monkeypatches Number of lines: 22 -*- coding: utf-8 -*- # Pry is a powerful alternative to the standard IRB shell for Ruby. It features syntax highlighting, a flexible plugin architecture, runtime invocation and source and documentation browsing.
As explained on on the Pry wiki:
The code show-source command is capable of showing source code for classes/modules and methods. Simply typing show-source method_name will pull the source for the method and display it with syntax highlighting. As a convenience, Pry looks up both instance methods and class methods using this syntax, with priority given to instance methods.
Here is an example:
// ♥ Pry  pry(main)> cd Pry  pry(Pry):1> $ From: /Users/edwardezekiel/.rvm/gems/ruby-2.3.3/gems/pry-0.11.3/lib/pry/pry_instance.rb @ line 24: Class name: Pry Number of monkeypatches: 6\. Use the `-a` option to display all available monkeypatches Number of lines: 643 class Pry attr_accessor :binding_stack attr_accessor :custom_completions
Pry Stack Explorer
Pry even lets you explore the stack that you’re working with to trace down tricky bugs. Stack trace methods include
cat --ex, and my personal favorite
wtf? This article by Benjamin Tan Wei Hoa provides some great stack trace examples.