Saturday, March 10, 2007

Shave That Yak



If you want to indulge your weakness for yak-shaving, you could start here. Wirble provides kick-ass enhancements to irb, including history, in the sense of the shell command history -- but it doesn't actually include a history command. It's utterly effortless to add one, however.

Here it is as a one-liner:

pp Readline::HISTORY.to_a

All you have to do to have a history command in irb is add that to your .irbrc file, located in your home directory, as a method called history. Unfortunately this yak is still kind of rough in patches; you'll get a bunch of quote marks and some additional noise in the output. Also, pp breaks Wirble's syntax coloring, and that's harder to fix than I'd like; pp is actually implemented directly with a stream, and getting to the string inside is harder than it should be.



More importantly, I haven't yet been able to define shell variables in irb, like !! and !$ in the shell, which give you the last command and the last argument in the last command, respectively. I'm so used to these in the shell that lacking them on the irb command line seriously throws me for a loop, and rather than getting used to it, I'd like to add them to irb (and thereby, of course, to script/console as well, which has to be the best, most underrated bit of brilliance in all of Rails).

The first problem is you can't actually name the methods !! or !$; you have to settle for h! and h?, but that's not a big deal. The trivial implementation is effortless; you use the code above for history and wrap that code in an eval(). Unfortunately, this simple-minded approach will kill your irb completely if you try to do it twice in a row, since the second time, it'll be eval()ing an eval(), and then you're trapped until you hit control-C.

The other thing is that I've been working on slightly the wrong problem. The last argument from your last statement in script/console, that's useful; but the functionality provided in the shell by backticks, which gives you the output of any given command, is very very handy in combination with !!. It would be better to be able to use that. I often use `!!` to mean "the thing I got back a second ago," and that kind of shorthand -- simply "the output you just returned" -- would be very handy in Rails console. It ought to be easy to get to, but I might have to patch irb itself in my ~/.irbrc to do it. I'm very much not sure if this is correct, but it appears that irb, even though it provides the return value of every operation (unless you tell it not to), does not actually capture that return value.

Anyway, this is my progress in the world of shaving that irb and script/console yak -- hopefully I'll have further progress to report at some point.

1 comment:

  1. I often use `!!` to mean "the thing I got back a second ago," and that kind of shorthand -- simply "the output you just returned" -- would be very handy in Rails console.

    Whoops - turns out that's already part of irb. Just do this:

    IRB.conf[:EVAL_HISTORY] = 1000 # ...or any size you want for the list of archived return values

    And you got it.

    ReplyDelete

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