Tuesday, October 2, 2007

Use vi (Or Any Text Editor) From Within irb

My presentation at Ruby East, How To Kick Ass With irb, turned out pretty well. Towards the end, somebody asked if it was possible to pop out of irb and into vi. With the help of several people in the audience, including Greg Brown from Ruport, I figured out that the answer was yes, and demoed it in three lines of code:

file = File.open("mytempfile", "w")
system("vi mytempfile")
eval(file.read)

I'm doing this from memory, so you may need to pass File.open different args, but it really is that simple. However, after the presentation, Greg came up and showed me a better way to do it using tempfile (from the Ruby standard library). The rest of the day was wrapped up in conference stuff, and the rest of the night spent in a search for pizza and a place to play Werewolf. I obtained the pizza with the help of Mike Milner, the other half of the Ruport team, and Greg solved the Werewolf problem by finding me in the hotel bar. The next morning, I was exhausted. I was on a shuttle back to the conference location at 8:30 am, which, as Ezra Z. later pointed out, translated to 5:30am in California time.

Anyway, Greg was on this shuttle too, along with Hal Fulton, and we started talking about the code we'd written the other day. Greg told me there was more hacking and less Werewolf at RubyConf, and this motivated us to start hacking. I kept hacking when I got to the airport, and on the plane home.

I think I finally have something bulletproof (or at least nicely robust). It reports syntax errors, supports multiple editors, and keeps your file available for a short time. This means that you can write your code from within irb. This interactive editor approach is actually faster than the red-green-refactor cycle, although obviously also less secure, since the real benefit of tests is the unbreakability and their function as the only documentation guaranteed to be true. It's best for code which is challenging enough that you want to test it out in irb as you write it, and involved enough that you want a record of what you've written.

Here's the code. If you also have Ben Bleything's irb history code, you may want to stay tuned, I think it's probably pretty easy to add the ability to automatically dump the contents of the file into your command history. (I know the reverse is true - I added a method to my irb which allows you to dump your command history to vi, and it was easy.)

4 comments:

  1. Hiya...

    good presentation last week. thanks for working this out. it is now a permanent part of my .irbrc


    bryanl
    http://smartic.us

    ReplyDelete
  2. I love using this for checking tweaks to a block of code. Many thanks. It also inspired me to write about my use of irb in emacs, which I use when I'm less sure what approach I'm going to use to solve a problem. From your article title, I'm guessing you're a vi guy, but hey, maybe you're bi-editorial(?). I have no idea what conjugation I should use for "edit" there.

    Anyway, thanks for the cool irb addition.

    ReplyDelete
  3. This is excellent. irb is fantastic for one-liner experiments but -- until now -- I found it cumbersome for anything longer. Now I can use it all the time.

    And I can unleash the full power of vi while editing. For what more could one ask?

    ReplyDelete

Note: Only a member of this blog may post a comment.