1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Spotlight on Gems: interactive_editor


Every once in a while, you come across a gem or library that seems so simple and natural, you think it aught to be a feature built into Ruby. Well, interactive_editor is one of those gems. In short, it allows you to launch an editor from IRB to edit your code, without having to rely on IRB's sketchy editing.

To see why this is really necessary, open up IRB and try to write a multi-line method. If everything goes well, you have a new method. However, if you have to make a change to this method, you have to type it all over again. Assuming your IRB has readline support, you can use the up arrow key to re-enter lines that don't need to be changed, but you still have to input each line of the method all over again. This, in short, is a major pain.

What interactive_editor does to fix this is allow you to launch an editor from IRB, to input or edit your code, and to exit the editor and return to IRB. Then, later on, you can launch the editor again and all your previous code is still there, so you can either add more code or edit existing code. It completely removes the entire shaky multi-line editing from IRB.


Installing interactive_editor is easy, it has no large dependencies and requires no native compilation.

$ gem install interactive_editor

You'll also need to have an editor you can launch from the command line. I personally use Vim, others like nano or one of the many other editors. There are many editors defined in the interactive_editor source code, but the easiest way to use an editor of your choice is to use the EDITOR environment variable. Set this variable to the editor of your choice either in your environment, or at launch.

Using interactive_editor

When you start IRB, you'll also need to require 'interactive_editor' as well. This can either be done after you start IRB, or while you start IRB like this.

$ irb -rinteractive_editor

If you also want to set a custom editor at launch, you it will look a little more like this.

$ EDITOR=vim irb -rinteractive_editor

Finally, time to edit something. Start up IRB as described above and run the ed method. This will run the editor described by the EDITOR environment variable. Once your editor launches, type in your code, save it to disk (it will be in a temporary file, you don't need to name it) and then exit the editor. That code you just typed in will be loaded in the IRB session. If you launch the editor again, it will display the previous code and allow you to make changes. Save the file and exit and those changes will be reflected in the IRB session.

$ EDITOR=vim irb -rinteractive_editor
> ed
# The editor launched here, and code was input
> say_hello
Hello, world!
=> nil

OK, so you've used this to write a tricky method, and you want to save your work. Simply open the editor again and save as a file in a known directory.

A Surprisingly Awesome Bonus

Again, this feature is so neat, you'd think they would have included it in IRB itself. You can not only edit code with this gem, but you can edit variables. Just try it, run "test".ed from an IRB session with interactive_editor. An editor launches with the YAML representation of the string in it, which you can edit and will be saved back to the variable. This is surprisingly useful for editing hashes and long strings.

  1. About.com
  2. Technology
  3. Ruby
  4. Tutorials
  5. Spotlight on Gems: interactive_editor

©2014 About.com. All rights reserved.