Ruby ships with an Interactive Ruby Shell (IRB) that every Ruby developer has played around with at some point. It’s a great place to experiment with unfamiliar methods and running bits of code, but if you really want to dig into an object or do hardcore debugging, it can leave a lot to be desired.
Pry is a great IRB alternative that has a number of features that make it one of my must-have tools.
Pry has five features that make it great:
1. View the documentation or source for a method
You may often find yourself wondering about the difference between methods like #reject and #reject!, but you don’t feel that it warrants a full Google search. Pry provides a show-doc method that makes this painless:
If you’re working with your own code and want to take a quick look at the source, there’s also show-method:
2. Search your Pry history
Pry’s hist --grep makes it easy to search through your Pry history:
3. Edit and reload files
Rather than switch between Pry and your editor, you can use edit to open up a file for editing:
The only “gotcha” is that you need to make sure you specify your editor in a .pryrc file:
Additionally, the -t flag opens up a temp file, giving you a full editor to define a method, class or other ruby object, if you find doing it line-by-line in Pry to be too tedious.
4. Run shell commands
You can run shell commands by prepending the command with a ‘.’. This is great when you’re working on a gem and want to edit a file without leaving Pry:
Any command with a ‘.’ in front will be forwarded to the shell, so you could even commit your code from Pry.
ls is useful for when you forget the name of that variable you defined earlier, and cd can be useful for diving into objects.
This feature is available in IRb as well as Pry, but it’s something that I use very often:
These are just the highlights of Pry and some of the commands I use. You can find more information on the commands on the Pry wiki and by using the help command in Pry. If you’re looking for more help with debugging, make sure you check out Aubrey‘s post on pry-remote.
These are my favorite Pry features—what are yours?