Location via proxy:   
[Report a bug]   [Manage cookies]                
Fork me on GitHub

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.

Quickstart

Install Pry:

$ gem install pry
$ pry

Try it out: (Displaying source for FileUtils.rm)

pry(main)> cd FileUtils
pry(FileUtils):1> show-method rm

From: /opt/ruby/lib/ruby/1.9.1/fileutils.rb @ line 556:
Number of lines: 10
Owner: FileUtils

def rm(list, options = {})
  fu_check_options options, OPT_TABLE['rm']
  list = fu_list(list)
  fu_output_message "rm#{options[:force] ? ' -f' : ''} #{list.join ' '}" if options[:verbose]
  return if options[:noop]

  list.each do |path|
    remove_file path, options[:force]
  end
end
pry(FileUtils):2>

For comprehensive coverage of Pry's features please check out the Wiki and watch the screencasts.

Introductory screencast

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.

Watch other screencasts

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.rb
require 'pry'

class A
  def hello() puts "hello world!" end
end

a = A.new

# set x to 10
x = 10

# start a REPL session
binding.pry

# program resumes here (after pry session)
puts "program resumes here. Value of x is: #{x}."

Pry session:

pry(main)> a.hello
hello world!
=> nil
pry(main)> puts x
10
=> nil
pry(main)> def a.goodbye
pry(main)*   puts "goodbye cruel world!"
pry(main)* end
=> nil
pry(main)> a.goodbye
goodbye cruel world!
=> nil
pry(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.

pry(main)> shell-mode
pry main:/home/john/ruby/projects/pry $ .cd ~
pry main:/home/john $ .emacsclient .pryrc
pry main:/home/john $ .cat .pryrc
def hello_world
  puts "hello world!"
end
pry main:/home/john $ load ".pryrc"
=> true
pry main:/home/john $ hello_world
hello world!

Please support Pry!

Pry was a huge amount of work and every dollar received is encouraging!