mmap.page

Dive into Pry

This note was written for Ruby < 2.0, thus outdated now.

Input

Special locals

Browsing code

Use cd to move into an object or scope. As in UNIX shells use cd .. to go back, cd / to return to Pry top-level and cd - to toggle between last two scopes. Complex syntax (e.g cd ../@x/@y) also supported.

Use whereami(alias @) to show code surrounding the current context.

Use show-source(alias $ or show-method) and show-code(alias ?).

Use find-method to recursively search for a method within a class/module or the current namespace.

Use stat method_name for basic method information.

Use ls to get a list of variables in current scope You can also use ls Class/Module, or ls variable.

Use watch to watch the value of an expression and print a notification whenever it changes.

cat automatic syntax highlighting for a number of file types.

cat accepts the following options:

Editing code

edit brings you to EDITOR. It accepts the following options:

Being run without any arguments, the edit command modifies the last input expression.

History

hist to display Pry history.

It accepts the following options:

Debug

Write binding.pry as a breakpoint in your code. When executing, it will stop here and open a Pry console.

Gems

Customization

For current session:

ruby
Pry.config.[option]

You can store Pry.config.[option] in .pryrc (project or global ~/.pryrc).

Plugins

pry-byebug

Use byebug (Ruby debugger) with pry.

I don't use it due to this bug. Besides, most of time, I just need pry-rescue.

pry-coolline

By default, code gets highlighted after you end your input. This plugin supports real time highlight.

pry-macro

Record and play macros. You can save macros to a file.

pry-rescue

Whenever an unhandled exception happens in your program, pry-rescue opens an interactive pry shell right at the point it was raised.

Install it together with pry-stack_explorer, then run rescue file.rb instead of ruby file.rb.

In the rescue Pry session, use up and down to move around the stack, and use wtf? or cat --ex to examine stack traces and the associated code. After identifying the problem, use edit-method (alias $) to fix the code, and try-again to verify the fix worked.

cd-cause lets you rewind back the previously raised exception. So, if you've rescued one exception, and then raised another (it happens…) you can jump back to the original cause of the problem.

You can also call Pry::rescue to control over which parts of your code are rescued:

ruby
require 'pry-rescue'

def test
  raise "foo"
rescue => e
  raise "bar"
end

Pry.rescue do
  test
end

You can also use Pry::rescue in the rescue clause:

ruby
def test
  raise "foo"
rescue => e
  Pry::rescued(e)
end

Pry::rescue{ test }

Testing

rspec
sh
rescue rspec
MiniTest

Add the following to your test_helper.rb or to the top of your test file

ruby
require 'minitest/autorun'
require 'pry-rescue/minitest'

It works perfectly with yard-doctest in terminal, but not is sublime-text.

Themes

In a pry session, run pry-theme list to list all installed themes.

Try a theme:

sh
pry-theme try pry-modern-256

If the theme is not installed, you can install it:

sh
pry-theme install theme-name

You can use uninstall to uninstall it.

Show current theme:

sh
pry-theme current

You can edit a theme:

sh
pry-theme edit

After you have decided your theme, write it in .pryrc:

ruby
Pry.config.theme = "pry-modern-256"

You can get more themes at Pry theme collection.