This page is not available in every language. Missing translations are disabled in the language selector.

Quick reference

Pry Ruby REPL Cheatsheet

A quick reference guide for Pry: the powerful Ruby runtime developer console, introspection, source inspection, navigation, and Rails debugging.

· Published

Pry is a powerful runtime developer console and IRB alternative for Ruby, offering source code browsing, syntax highlighting, and state navigation.

Basic Navigation & Context Switching

# Change scope into an object or class (like directory navigation)
[1] pry(main)> cd Array
[2] pry(Array):1> cd- ..
[3] pry(main)> cd User.first

# Check current binding nesting depth
[4] pry(User):2> nesting

Code & Documentation Introspection

# View source code of a method directly in console
show-source Array#select
show-source User#full_name

# View YARD / RDoc documentation for a method
show-doc Array#compact

# Search for methods matching a query
find-method each

Inspecting Variables & Methods (ls)

ls           # List methods, locals, and instance variables
ls -m        # List instance methods defined on current object
ls -M        # List class methods defined on current class
ls -l        # List local variables
ls -i        # List instance variables (@vars)
ls -c        # List constants defined in scope
ls -G regex  # Filter output matching regular expression

In-Session Debugging

# Drop into Pry from anywhere in Ruby code
require 'pry'
binding.pry
  • wtf?: Print backtrace of the most recent exception.
  • hist: Display input history.
  • edit Object#method: Open method definition directly in your configured $EDITOR.

Rails Integration (pry-rails)

Add pry-rails to your Gemfile:

group :development, :test do
  gem 'pry-rails'
end

In rails console:

show-models      # Print all database schemas and relationships
show-routes      # Print routing table
show-middleware  # Print Rack middleware stack

References

Related topics