Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@SteveBenner

This comment has been minimized.

Copy link

@SteveBenner SteveBenner commented Dec 24, 2014

Thanks for putting this up! I ‘d but also modified it to add syntax highlighting, which I think makes it quite a bit prettier!

Simple regex I used: (?<=^- )(.*)(?= *--+ )

@bsimpson

This comment has been minimized.

Copy link

@bsimpson bsimpson commented Feb 23, 2015

Just a note on showing the backtrace: The command is _ex_.backtrace. Looks like markdown ate the underscores

@stujo

This comment has been minimized.

Copy link

@stujo stujo commented May 21, 2015

Thank you!

@geocodinglife

This comment has been minimized.

Copy link

@geocodinglife geocodinglife commented Apr 10, 2016

Info very helpful,
Thanks!!!!

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Jun 15, 2017

very helpful, thanks

@cmdallas

This comment has been minimized.

Copy link

@cmdallas cmdallas commented Mar 9, 2018

This is amazing. Thanks!

@hailengc

This comment has been minimized.

Copy link

@hailengc hailengc commented Mar 26, 2018

bookmarked

@abdul-shajin

This comment has been minimized.

Copy link

@abdul-shajin abdul-shajin commented Dec 6, 2018

Thanks for this.

@mrinterweb

This comment has been minimized.

Copy link

@mrinterweb mrinterweb commented Feb 1, 2019

One trick I use all the time is ls -G <search-term> <Object> It greps method names on an object containing the term.

@maxkerp

This comment has been minimized.

Copy link

@maxkerp maxkerp commented Feb 14, 2019

This is pure gold 👌. Thank you so much for this! 😁✌️

@OddEssay

This comment has been minimized.

Copy link

@OddEssay OddEssay commented May 23, 2019

Thanks for this! I spent so long trying to find how to skip inspecting the result of a slow command (By adding ; to the end)

For context in case it helps someone:

Model.all.count # Fast

If you break it down by line, Pry wants to inspect the interim step which means serializing every record in your table

q = Model.all # Slow, as it tries to inspect every row in the table
q.count # Fast

So, you can make it quick by skipping output:

q = Model.all; # Fast again
q.count # Fast
@misraelson

This comment has been minimized.

Copy link

@misraelson misraelson commented Aug 7, 2019

What about exit or escape commands? Sometimes you are trapped in a pry inside a loop and want to escape all

@Reksford

This comment has been minimized.

Copy link

@Reksford Reksford commented Aug 8, 2019

If you want to escape out of the program use exit-program or simply !!!

@misraelson

This comment has been minimized.

Copy link

@misraelson misraelson commented Aug 8, 2019

@gumatias

This comment has been minimized.

Copy link

@gumatias gumatias commented Dec 27, 2019

I'm inside a giant loop, is there a way to "continue all"? exit! ends up killing rails server altogether.

@Patrickmnts

This comment has been minimized.

Copy link

@Patrickmnts Patrickmnts commented Jan 15, 2020

@gumatias disable-pry will skip all future bindings without killing the server.

@gumatias

This comment has been minimized.

Copy link

@gumatias gumatias commented Jan 16, 2020

thanks!

@mel-delosada-wb

This comment has been minimized.

Copy link

@mel-delosada-wb mel-delosada-wb commented Mar 9, 2020

Much appreciated!

@QasimQureshi

This comment has been minimized.

Copy link

@QasimQureshi QasimQureshi commented Mar 10, 2020

Want to stop the code from running after pry, without quitting the server? I use raise-up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.