Everything you wanted to know about pry, but were too afraid to drop a binding.pry into
Pry was created by John Mair (NOT Mayer)
Here’s his twitter: https://twitter.com/banisterfiend
I wasn’t able to find anything on why he decided to make this, what his motivations are or his history, but he does state in the documentation of pry that he wants it to eventually have the amount of functionality of lisp, so I think its safe to conclude he wasn’t happy with the options out there and wanted to improve on the standard REPL, for instance IRB.
Reminder: IRB is the interactive Ruby shell, and allows us to write, test and modify code as we go.
Pry is a REPL, or read-eval-print-loop, also known as an interactive top-level or language shell. A REPL takes single user inputs (or single expressions), evaluates them and returns the result to the user.
Pry is like IRB but with a lot more functionality, like saving history between sessions, auto-complete, syntax highlighting and auto indentation.
Ok, enough with the histrionics, here are 5 cool and (probably) lesser known tricks for Pry:
Since many of our use cases will involve using pry’s ability to ‘pause’ our programs by dropping a binding.pry into them and not necessarily utilizing pry on its own, I’ll gear these tips towards that way of using it. Suffice it to say however that pry can indeed be used independently, on its own, to write, experiment and even bring code out of pry into a file, instead of the other way around.
1 Search your history- hist –grep makes it easy to search through your pry history-
6] pry(main)> def foo
 pry(main)* puts “whahwa”
 pry(main)* end
 pry(main)> show-method foo
From: (pry) @ line 1:
Number of lines: 3
 pry(main)> hist –grep foo
6: def foo
9: show-method foo
2 ‘Show-method’ which will show you the source of a method (usually used for something you wrote)
3 Mess up a line of code and can’t figure out how to go back? Here’s how –
amend-line N replacement code where N is the number of the line (or range of lines using A..B syntax) you wish to replace. The amend-linecommand is usually used in concert with show-input command.
4 The play command -If you’re confident that your binding.pry is in the right place but don’t know exactly where your code is going wrong, the play command might be super helpful. This command enable you to replay code from files and methods as if they were input directly into the REPL. Supported options:
Use the -l option to restrict a subset of lines to be played
Use the -o option to play Pry’s output history
Use the -i option to play Pry’s input history. This can only replay pure Ruby code, not Pry commands.
Use the -s option to select the super method. Can be repeated to traverse the ancestors
Use the -d option to play documentation for methods
Use the –open option to play the selected content except the last line (which is usuallyend). amend-line can then be used to modify the method
For instance, if you’re in pry after hitting a binding.pry (on line 5), you can type in ‘play -l 6’ and pry will then run the code on line 6. Pretty cool.
5 the -wtf?(??????!!) command
The creator of Pry seems to have a pretty good sense of humor, as finding the backtrace for an error requires you to type it wtf? Further, you can even add ? or ! to your command to see more lines.
Of course, the best documentation comes from the source, so if you’d like to find more tricks or get more specific with the ones I described above, head over to the pry wiki at https://github.com/pry/pry/wiki/User-Input on github. Happy prying!
Further resources on pry:
Great talk by Conrad Irwin about REPL driven development with Pry: https://www.youtube.com/watch?v=D9j_Mf91M0I