# iain.nl  

Customizing IRB, 2010 edition

Written on

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.