Customizing IRB, 2010 edition
TL;DR: Check out my new .irbrc-file!
Customizing my work environment is a nerdish hobby of mine. I spend far to much time tweaking my terminal. While I’ll save my terminal customizations for another time, I’ll show you my IRB tweaks in this post.
There are several tools to improve your IRB, and some of them have been around for ages. But the arrival of Bundler makes it difficult to use them. Bundler creates a bubble in which you have to specify your dependencies explicitly. Furthermore, with project specific gemsets, provided by the ever so awesome RVM, we need to install these IRB extensions for every project.
This means that you cannot be sure that extensions like Wirble are available in your new and shiny Rails console. There is only one way around that: add them to your Gemfile. This is what I usually add:
group :development do gem "wirble" gem "hirb" gem "awesome_print", :require => "ap" gem "interactive_editor" end
Extension Loading
To load the IRB extensions without blowing up in your face when they’re not available, I gently try to load them, and configure them only when that is successful. You can download my .irbrc on github. Here is what it looks like:

When you start IRB, it shows a line with the extensions loaded. If it’s gray, it’s not appropriate (like rails2 in this example), loaded extensions are green and extensions that are not available are in red.
Showing Queries in ActiveRecord 3
As you can see, the queries done by ActiveRecord are displayed in the same way as they are displayed in your log files. In Rails 2, you would’ve done this by redirecting the log output to STDOUT. In Rails 3 you need to subscribe to the ‘sql.active_record‘-notifications.
This could in theory also be done for other Rails 3 compatible ORMs like Mongoid, but I haven’t looked into that yet.
Hirb
Hirb formats objects into pretty tables, as you can see in the picture above. It also provides some scrolling possibilities like the command line tools less and more. Very handy!
Wirble
The first IRB extension anyone uses. Wirble provides you with history and syntax highlighting.
Awesome Print
While Wirble colorizes the output to improve readability, it can get cluttered really fast, especially when you’re dealing with nested hashes and arrays. AwesomePrint helps to untangle your object mess:

Print Methods
The ‘pm‘-extension I found on the intertubes some time ago, lists the methods and what arguments they take on any given object. You can filter it, by providing a regex. This is what it looks like:

It’s not a gem, but a snippet pasted directly into my irbrc, so it’s always available.
Interactive Editor
Open vim (or any other editor) from IRB, edit your code, save it, close your editor and the code gets executed. Open vim again and your code is visible and editable again. Very awesome! Check it out!
More
Yeah, there more. There’s bond, which makes autocompletion better, and utility belt, and more. I can’t remember to use them all, so I haven’t included them into my irbrc. They certainly are cool enough for you to check out! Also, a lot of great tips are here on Stack Overflow.
If you have any good tips, please share them! Oh, and the other OSX tweaks I use are on github.
PS. For those that don’t know how to load this: put the .irbrc file in your home directory and it will load automatically.
TweetView Comments to 'Customizing IRB, 2010 edition'
Leave a Reply
You must be logged in to post a comment.
Very nice! I think I will steal your .irbrc ;)
Diederick Lawson
24 Jul 10 at 6:13 PM
Just put it back when you’re done with it…
Iain Hecker
24 Jul 10 at 6:55 PM
using Disquss :)
iain
25 Jul 10 at 11:23 AM
[...] Customizing IRB, 2010 edition at Adventures with Ruby – Updated .irbrc to include a bunch of new hotness for your irb sessions [...]
Link dump for July 24th through July 25th | The Queue Blog
26 Jul 10 at 5:05 AM
I just stole it from Diederick and it looks really nice. Sweet!
Kevin
27 Jul 10 at 3:00 PM
On OS/X you'll want to enable “Use bright colors for bold text” in the Terminal preferences. Else some stuff will be invisible.
ariekanarie
28 Jul 10 at 9:23 AM
Utility belt doesn't seem to be loaded – any tips? looking for things like grep_classes or user(1) for User.find(1).
I had trouble getting it to load by just putting in 'require utility_belt', so something else must be broken with it, perhaps associated with the new rails3 console?
Loving the other stuff though, thanks so much!
Kevin
28 Jul 10 at 10:23 PM
I didn't add utility belt to my irbrc, because I couldn't remember to use it. To use it, add it to your Gemfile if you're using bundler, and put this at the bottem (but before the last “puts” statement)
extend_console “utility_belt” do
# …some config here if you wish…
end
iain
29 Jul 10 at 7:35 AM
Aah thanks. Turns out my problem was that utility_belt only 'equips' some of the features by default (the lame ones it seems like?) So I had to do UtilityBelt.equip(:language_greps) separately. And rails_finder_shortcut
Thanks again for the great console, I'm enjoying it!
kevin
13 Aug 10 at 6:59 AM
Does anybody know, why hirb doesn’t work with
instead works.
beYou media
15 Aug 10 at 11:36 PM
another question :-)
how did you set those gorgeous terminal colors?
THNX!!
beYou media
15 Aug 10 at 11:42 PM
found a solution myself:
Tag.limit(3).all
works
beYou media
15 Aug 10 at 11:43 PM
There are some hacks and the ir_black theme for the terminal. See http://blog.infinitered.com/entries/show/6
iain
16 Aug 10 at 7:37 AM
the reason for this is as follows: Tag.limit(3) returns a ActiveRecord::Relation, which hirb does not know, so it won’t make it into a table. It switches to default behavior, which is calling #inspect on it. This means that the query gets executed and the results shown. We’re already past hirb, so it will output like it used to.
The .all method (alias of .to_a) will execute the query and return an array, which hirb knows.
Time for a patch to hirb.
iain
16 Aug 10 at 7:50 AM
I want to know, how did you make color highlighting?
Anonymous
18 Aug 10 at 2:32 PM
color highlighting is done in part by wirble and some of the other components; the irb prompt is colored by myself.
iain
19 Aug 10 at 7:29 AM
Do you know, how to force HIRB to use table display for Rails3 ActiveRecord::Relation? Whenever I use Model.where() or Model.limit() I have to prefix it with “table”. Any idea how to make things work ootb?
Mdrozdziel
1 Sep 10 at 8:07 AM
Append .all to your relation/scope, e.g. “User.active.all”, this will turn it into an array, which HIRB understands. The author of HIRB has no intention of making a special case for ActiveRecord::Relation objects, afaik.
iain
1 Sep 10 at 12:36 PM