«

»

Aug 17

.irbc contents

I think that irb is one of the most useful tools in the rails toolbox. It allows me to quickly determine how something works, sample results (for putting into blogs, don’tcha know) and makes me a more effective ruby developer.

One of the nice things about irb is it’s .irbrc file where you can place startup commands.  I’d like to share mine (and ask for others to share their favorite .irbrc tools/contents.

As of today, my .irbrc looks like this:

Here’s what it does:

1

Require’s rubygems — several of the following lines refer to gems, so this is needed in order to find the data

2

Wirble provides some very useful tools for irb — from colorizing to shortcut methods. One of the shortcut methods allows you to see the methods an object has:

More information can be found at http://pablotron.org/software/wirble/.

3

map_by_method allows me to say

instead of having to say:

You can also do people.map_by_name_and_age if the elements of the people array quack respond to age. You can install map_by_method by typing

4

what_methods allows me to do the following to determine which method(s) supply a particular result:

It can be installed with

5

pp is part of the default ruby libraries. It allows you to “pretty print” objects.

6

This line will automatically indent code as it is typed into irb.

8

This line initializes wirble.

9

This line allows wirble to use color — I’ve turned it off for now; running irb in emacs leaves lots of terminal code artifacts when this is turned on.

12-16

This method is a little something I wrote which shows the methods of an object — those which it does not have in common with Object. It also sorts them — which was suggested by Joe OBrien of The EdgeCase.

So, what’s in your .irbrc?

2 comments

  1. Chris

    You can see my .irbrc at http://code.tie-rack.org/cms/irbrc.

    Mine is wrapped in an unless condition to make sure that Rails’ script/server isn’t running (up until Rails 2.0, I think, script/server would load .irbrc at the beginning of every request, which could cause issues). I share this across multiple machines, some of which still use Rails 1.2, so that’s still necessary.

    I’ve also (at the end there) got some gems that load only if I’m currently using Ruby 1.8.6 and not JRuby.

    And I use a little IRB.conf[:IRB_RC] trick to get one gem to load after everything else (including Rails) so that it doesn’t mess up some of Rails’ magic.

  2. Matt Williams

    Thanks for sharing! There’s a lot of neat ideas there. I plan to incorporate some of them.

Leave a Reply

%d bloggers like this: