Tweaking IRB

Posted by Nick Sieger Sun, 23 Apr 2006 04:02:00 GMT

IRB (Interactive Ruby Shell) is one of those tools that a hacker learning Ruby hopefully discovers right away. It’s an extremely useful way to learn the language, verify hunches, test assumptions, and get immediate feedback. IRB promotes learning by doing, which is the best way of making something stick in your head. (You can even try an online version of irb without even installing Ruby!)

The first order of business when using IRB is to setup your preferences. If you haven’t done so already, create the file ~/.irbrc (%USERPROFILE%.irbrc on windows native ruby). .irbrc is just a regular ruby script where you can run arbitrary ruby code at the start of your IRB session. Add the following to .irbrc:

require 'irb/completion'
ARGV.concat [ "--readline", "--prompt-mode", "simple" ]

This sets up usage of readline in your session and turns on TAB completion, making IRB feel as comfortable as regular old bash. Now you can type Kernel::<TAB> and get a list of available methods! Good.

Next, the thing that you find yourself doing after using IRB for a while is cutting and pasting code from your console buffer over to your text editor. Don’t have Ruby’s reflection rules down yet? Not sure whether to use instance_eval or module_eval when working on that metaprogramming hack? Working inside Rails’ script/console and searching for the right ActiveRecord finder options? No matter how good your terminal program, you probably have to use the mouse to select text out of it to copy to your text editor, and hackers hate having to switch from the keyboard to the mouse when in the flow of programming.

So here’s a technique that will append commands entered in your IRB session to a file in your home directory (idea from ruby-talk:58931). Put the following in your .irbrc:

module Readline
  module History
    LOG = "#{ENV['HOME']}/.irb-history"

    def self.write_log(line)
      File.open(LOG, 'ab') {|f| f << "#{line}
"}
    end

    def self.start_session_log
      write_log("
# session start: #{Time.now}

")
      at_exit { write_log("
# session stop: #{Time.now}
") }
    end
  end

  alias :old_readline :readline
  def readline(*args)
    ln = old_readline(*args)
    begin
      History.write_log(ln)
    rescue
    end
    ln
  end
end

Readline::History.start_session_log

Now every line typed into IRB will immediately be saved into ~/.irb-history. Exercise left to the reader to bind a custom keystroke and macro to yank the last line out of that file and automatically paste into your text editor.

Long-time bash users know that the shell maintains a history of commands across sessions so that you can access commands you typed yesterday. Wouldn’t it be nice to do this in IRB as well? Wish granted:

require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"

Happy ruby hacking! If you find any more handy IRB tips leave them at rubygarden, and let me know about them.

Footnote: I realize there is duplication and non-DRY happening here with two copies of your IRB history, but I came across these techniques at two different times, and the functions they serve seem different enough to potentially use them both. If you don’t like that, choose whichever is more appropriate for your needs.

(Hope this post serves your needs Dan.)

Posted in  | 2 comments | no trackbacks

Hello, is this thing on?

Posted by Nick Sieger Fri, 21 Apr 2006 04:00:20 GMT

Hello there, gentle reader! Thanks for stopping by and helping me re-establish my own corner of the web.

My name is Nick. I’m a hacker originally from and currently located in Minneapolis, MN. I experimented with blogging for a while at my Radio weblog, Klactoveesedstene and finally decided that I need to reset and try this thing again.

My current interests are fairly broad, but to list a few specific examples:

  • Ruby and dynamic languages
  • Language design, domain-specific languages, and expressiveness in programming
  • Less code and DRY
  • Javascript as a serious programming language, AJAX, Rails, Greasemonkey
  • SCM, continuous integration, build tools (maven2, rake)
  • Testing methodologies: developer testing, TDD, BDD, web testing (watir), FIT
  • and (the famous) much much more...

A bit about the setup here. First, a plug for the folks at Rimuhosting -- I’ve got a VPS with Rimu. They set it up for me within about an hour of placing an order (on a Saturday, no less!), and I was off and running. I chose Ubuntu Breezy for my distro, and it came spanking clean -- extremely minimal, so that I can practice additive rather than subtractive security on my server and install only what I need. I contemplated going with shared hosting for a little while but given that I like to tinker with stuff I’m really glad I chose a setup with root access.

First order of business when setting up the server is to get Subversion, Trac, and ruby. Building my own Ruby 1.8.4 proved a bit of an extra challenge since things you’d think were pretty essential in a distro like openssl, gdbm and zlib were actually missing, but they were easily apt-gettable. By the end of the weekend I had purchased domains from Go Daddy, assigned the DNS entries in Rimu’s control panel, had Apache2 up and running with my very own svn, trac and typo instances running. Very slick!

And now in the words of Guy Kawasaki, I hope to use the word “I” a lot less if possible on this blog. Back to your regularly scheduled hacking...

no comments | no trackbacks

Older posts: 1 ... 15 16 17