Pry 102: Advanced Features

Many of you have read my previous posts about the ruby REPL Pry. If not, you should check them out: Give it a Pry and Make It Your Own. During the months since writing those articles, Pry has made major strides in many areas. I'd like to spend the next few minutes going through some of the newer features and re-invigorate your interested in Pry. Remember, just like before, Pry has amazing documentation, helpful maintainers, and solid community backing. So be sure to look around for additional information.

This post is a little code heavy. Beware... :)

1.) Input Buffer Manipulation

# (Section References)

Writing multi-line commands in a REPL can often be quite annoying. Usually, if you make an error on any line, you'll need to rewrite from the beginning. This is why I typically recommend integrating your REPL with a gem like interactive_editor, which gives you a tempfile to edit in your default editor and calls eval() when you close. With Pry, you have these features built in along with several additional features.

Before we move on, let's cover Pry's prompt. The Pry prompt has a lot of information available for you at a glance.

[1] pry(main)>

The number is the current input buffer line number expression number and it will increment as you enter more lines another expression. Many Pry commands will allow you to specify the line you'd like to work with by setting the -i flag and passing the input buffer line number expression number. The word in parenthesis is the current self and can be changed using the cd and cd .. commands.

edit - John Mair asked me to correct my usage of the term input buffer line number. I've made corrections through the post but to summarize: The number in the Pry prompt is not the input buffer line, but rather the expression number. In a multi-line expression (for example a method definition) each line associated is assigned a shared expression number which increments per expression. In short, it's not a line number.

Be sure to set your default editor in your ~/.pryrc file by adding the following line Pry.config.editor = "mate"

Ex. 1 (amend-line)

amend-line command which allows you to modify specific lines in your input buffer.