Stupid IRB Tricks

This post reflects some tricks I sent out in a mail to PDX.rb and some others I posted on my Intel internal Ruby blog. There’s nothing terribly novel here, but if you haven’t stumbled on these yet, they might save you some time.

Your .irbrc file gives you a lot of control over what your IRB looks like each time it starts. Here’s what my .irbrc file looks like:

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

class Object
  def mymethods
    (self.methods - self.class.superclass.instance_methods).sort
  end
end

The first two lines turn on tab completion. If you don’t have this on already, turn it on now! It only works when IRB can figure out the type of expressions, but it helps make the interpreter more friendly. Type []. and hit tab to see the array methods tab complete.

The second chunk sets up an useful introspection function that Ben and I came up with. It allows me to type foo.mymethods and get only the methods that are defined for foo, but not the methods defined in its superclass, which I find is often what I want (and sorted!). This is important because sometimes Ruby’s humane interface means the number of methods can be a little overwhelming.

Oh, and one other tip! If you’re doing interactive shell scripting in irb, you’ll often get into situations where the huge list of files you’re copying, or text-replacing or whatever is printing after every command since most Ruby commands return values. Waiting for hundreds of lines to print after each command can start to drive you nuts. Thankfully, you can shut off this echoing behavior in irb by typing:

conf.echo = nil

Those are all the tricks I can think of, but there must be more out there…