Interactive editing in irb – test ideas like a pro

by Ochronus on June 29, 2011

I’m sure you’ve been in the situation you had to test some ideas quickly, but irb itself was not enough because the code was too complicated for a simple run-while-typing scenario, so you’ve created your Nth test.rb and ran ruby test.rb for the gazillionth time. You must have wondered if there’s a better way. Aye. There is. It’s called interactive_editor and it’s a gem. In both meanings :)

You can do a simple gem install interactive_editor . If you want to take a closer look, see https://github.com/jberkel/interactive_editor.

What this gem does is basically allowing you to launch an editor from your irb session and edit your code there, which is run upon exiting from the editor. If you need to modify, just relaunch the editor and you’re presented with the code again. Priceless. In pictures – click the image for a real-world size:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Then you want to modify the code:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Oh, and interactive_editor supports vim, mvim, vi, emacs, nano and textmate. Nice.

Another tip: you can just add this to your .irbrc so it gets auto-loaded on every irb launch:

 

require 'rubygems'
require 'interactive_editor'

Interactive_editor leaves no trash behind, it operates using temporary files. Nice and clean.

Previous post:

Next post: