Editing with Pry

Pry Bar & Wire Brush by Scott Hart is licensed under CC BY-NC-ND

I’ve already touched a bit on the power of pry in a previous post back in 2016. I want to revisit pry to expand on a new functionality that I have since started using – its ability to edit.

More than One Line?

In my personal experiences, I’ve constructed some long one-liners while in irb or pry. These interactive sessions do not provide a great experience for multi line commands, as you are restricted to editing only one line at a time.

Fortunately, in pry, there is the edit method. This opens up a temporary file (pry’s input buffer) with a more capable editor for your task. You can configure the editor by executing the Pry.config.editor = "vim" statement, although your environment variable EDITOR is used by default. If you want, you can even use a GUI editor like Atom (you’ll need to use atom --wait for this to work). If you have a specific preference, it makes sense to add this configuration in your .pryrc so it is configured when pry loads up.

By default, edit will open the last executed statement issued in your session. If you want to edit with a blank canvas, then you can use the edit -t to open a temporary buffer. You can even get fancy and re-edit older inputs using edit --in <input-number> (i.e., edit --in -2 – the second last input).

Using a full editor is great for those more complex statements. Now you can break up statements into multiple lines to make it easier to comprehend and create. In the event you were slightly wrong, you can simply edit again and fix up your statement. This is great for iteratively building longer statements. It is a common style in Ruby to use indentation when using blocks and defining classes/methods/modules, so take advantage of edit.

The following demonstrates pry’s multi-line editing functionality.

Patching Objects

The edit method has one more amazing trick up its sleeve – patch editing. pry now gives you the ability to modify existing object method definitions at runtime. To quote the help text for the edit --patch:

Instead of editing the object’s file, try to edit in a tempfile and apply as a monkey patch

As the patch is only for the pry session, there is a greater sense of exploring and less worry surrounding the commitment of the edit.

Some caveats that I’ve hit with this approach is that you can only patch <class>#<method> (instance method) or <class>.<method> (class method). You might want to edit the class itself, but you’ll hit NotImplementedError: Cannot yet patch #<Pry::WrappedModule:0x007fcc32afecd0> objects!. Unfortunately, I’ve not yet found a nice way to get around this (so let me know if you have a way!). I know that you can modify the file itself (i.e., edit app/models/model.rb) but that’ll actually write to the file – I would want it done as a patch.

The following demonstrates pry’s patch editing functionality.