Skip to content

Instantly share code, notes, and snippets.

@lfender6445
Last active March 28, 2024 08:38
Show Gist options
  • Save lfender6445/9919357 to your computer and use it in GitHub Desktop.
Save lfender6445/9919357 to your computer and use it in GitHub Desktop.
Pry Cheat Sheet

Pry Cheat Sheet

Command Line

  • pry -r ./config/app_init_file.rb - load your app into a pry session (look at the file loaded by config.ru)
  • pry -r ./config/environment.rb - load your rails into a pry session

Debugger

  • help ls -- Display command options for pry command ls
  • ls <Object> -- Show all of the available methods that can be called by an object
  • _ -- Last eval
  • ? <Object> -- Shows more information (doc) about an object, or method
  • _file_ -- Represent the last file Pry touched
  • wtf? -- Print the stack trace, same as _ex_.backtrace
  • $ -- Show source, shortcut for show-source
  • edit Class -- Open file in $EDITOR
  • edit Class#instance_method -- Open file in $EDITOR
  • <ctrl+r> -- Search history
  • _out_ -- Array of all outputs values, also _in_
  • cd <var> -- Step into an object, change the value of self
  • cd .. -- Take out of a level
  • binding.pry -- Breakpoint
  • edit --ex -- Edit the file where the last exception was thrown
  • .<Shell> -- Runs the command
  • whereami -- Print the context where the debugger is stopped
  • whereami 20 -- Print the context 20 lines where the debugger is stopped
  • ; -- Would mute the return output by Ruby
  • play -l -- Execute the line in the current debugging context

pry-nav

  • next -- execute next line
  • step -- step into next function call
  • continue -- continue through stack

pry-rescue

  • rescue rspec -- break on exception in rspec
  • rescue rails server -- break on exception in rails server
  • try-again -- run last failing spec, reloads the file not the enviornment
@DavidEcklund
Copy link

Thanks for this!

@ssoulless
Copy link

very useful!!! Thank you!

@apatniv
Copy link

apatniv commented Dec 11, 2021

Useful

@davidr64
Copy link

davidr64 commented Feb 4, 2022

pry-backtrace is a great option for viewing the stack. It will include line numbers where I don't think wtf? does

@dev-elle-up
Copy link

If you want to make changes to your code while paused with pry, you can type reload-code into your terminal and it will reload the file you're paused in. (Save changes to the file first).

@ducmanh2111
Copy link

should add @ keyword to the alias of whereami ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment