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.
It is recommended you watch the following screencast put
together by Josh
Cheek. It includes instructions on installing Pry as
well as some coverage of core features.
Pry is written from scratch to provide a number of advanced features,
some of these include:
Source code browsing (including core C source with the pry-doc gem)
Navigation around state (cd, ls and friends)
Documentation browsing
Live help system
Open methods in editors (edit-method Class#method)
Syntax highlighting
Command shell integration (start editors, run git, and rake from within Pry)
Gist integration
Runtime invocation (use Pry as a developer console or debugger)
Exotic object support (BasicObject instances, IClasses, ...)
A powerful and flexible command system
Ability to view and replay history
Many convenience commands inspired by IPython, Smalltalk and other advanced REPLs
Pry also aims to be more than an IRB replacement; it is an attempt to
bring REPL driven programming to the Ruby language. It is currently not
as powerful as tools like SLIME for lisp, but that is the general
direction Pry is heading.
Pry is also fairly flexible and allows significant user customization
making it a good choice for implementing custom shells.
More examples
Runtime invocation
Pry can be invoked in the middle of a running program. It opens a Pry
session at the point it's called and makes all program state at that
point available. When the session ends the program continues with any
modifications you made to it.
This functionality can be used for such things as: debugging,
implementing developer consoles and applying hot patches.
# test.rbrequire'pry'classAdefhello()puts"hello world!"endenda=A.new# set x to 10x=10# start a REPL sessionbinding.pry# program resumes here (after pry session)puts"program resumes here. Value of x is: #{x}."
Pry session:
pry(main)>a.hellohelloworld!=>nilpry(main)>putsx10=>nilpry(main)>defa.goodbyepry(main)*puts"goodbye cruel world!"pry(main)*end=>nilpry(main)>a.goodbyegoodbyecruelworld!=>nilpry(main)>x="changed"pry(main)>exit# OUTPUT: program resumes here Value of x is: changed.
Command Shell Integration
A line of input that begins with a '.' will be forwarded to the command
shell. This enables us to navigate the file system, spawn editors, and
run git and rake directly from within Pry.
Further, we can use the shell-mode command to incorporate the present
working directory into the Pry prompt and bring file name completion.
We can also interpolate Ruby code directly into the shell by using the
normal #{} string interpolation syntax.
In the code below we're going to switch to shell-mode and edit the .pryrc
file in the home directory. We'll then cat its contents and reload the
file.