- Invoking pry debugging
- Callstack navigation
- Short commands
- Repeat last command
To invoke the debugger, place
binding.pry somewhere in your
code. When the Ruby interpreter hits that code, execution will stop,
and you can type in commands to debug the state of the program
byebug has a very similar interface as
byebug does not
use the powerful Pry REPL.
binding.pry uses Pry, but lacks some of the
features. GitLab uses the
gem. This gem brings some capabilities
using that, will give you the most debugging powers.
Check out the docs for the full list of commands.
You can start the Pry REPL with the
There are a lot of features present in
pry, too much to cover in
this document, so for the full documentation head over to the Pry wiki.
Below are a few features definitely worth checking out, also run
help in a pry session to see what else you can do.
With the state navigation you can move around in the code to discover methods and such:
# Change context  pry(main)> cd Pry  pry(Pry):1> # Print methods  pry(Pry):1> ls -m # Find a method  pry(Pry):1> find-method to_yaml
You look at the source code
 pry(main)> $ Array#first # The above is equivalent to  pry(main)> cd Array  pry(Array):1> show-source first
$ is an alias for
Similar to source browsing, is Documentation browsing.
 pry(main)> show-doc Array#first
? is an alias for
With Ctrl+R you can search your command history.
To step through the code, you can use the following commands:
break: Manage breakpoints.
step: Step execution into the next line or method. Takes an optional numeric argument to step multiple times.
next: Step over to the next line within the same frame. Also takes an optional numeric argument to step multiple lines.
finish: Execute until current stack frame returns.
continue: Continue program execution and end the Pry session.
You also can move around in the callstack with these commands:
backtrace: Shows the current stack. You can use the numbers on the left side with the frame command to navigate the stack.
up: Moves the stack frame up. Takes an optional numeric argument to move multiple frames.
down: Moves the stack frame down. Takes an optional numeric argument to move multiple frames.
frame <n>: Moves to a specific frame. Called without arguments will show the current frame.
When you use
binding.pry instead of
byebug, the short commands
c do not work. To reinstall them, add this
if defined?(PryByebug) Pry.commands.alias_command 's', 'step' Pry.commands.alias_command 'n', 'next' Pry.commands.alias_command 'f', 'finish' Pry.commands.alias_command 'c', 'continue' end
You can repeat the last command by just hitting the Enter
key (e.g., with
next), if you place the following snippet
Pry::Commands.command /^$/, "repeat last command" do _pry_.run_command Pry.history.to_a.last end
byebug supports this out-of-the-box.