⇧ home

FastRI for the win

By Mislav on 11 Mar 2008 in rails

Ruby can be wicked cool from the console. A couple of its well known utilities are irb (the interactive Ruby shell) and ri, a documentation lookup tool.

Although the former is incredibly useful, I doubt that ri sees much usage among the professionals—the biggest reason being that it is awfully slow.

$ ri Array#compact

# ... tapping my fingers for several seconds ...

It is also bugged in certain obscure ways.

Well, looks like Mauricio Fernandez shares our pain. He has created FastRI: faster, smarter RI docs for Ruby.

This isn’t new. It was released more than a year ago, actually—I just can’t figure out how it went under the radar for so long. Today a member of Caboose pointed it out to me. Let’s install it, shall we?

There are 2 installation methods: gem vs. tarball. I went with gem, but the author recommends the tarball for performance purposes.

Once it’s installed, we need to build the index first:

$ fastri-server -b

# ... takes some time and writes to ~/.fastri-index

When it’s done, we’re ready to use it the same way we used ri. I’m going to use the qri executable which defaults to local mode (read the article carefully for more info on local/remote mode).

qri compact

Fast … and with syntax coloring, too!

Docs in the interactive shell

There is more; often you are in irb and want to look up documentation while experimenting with code. Mauricio has written a follow-up post in which he explains how to get in-shell documentation with tab completion. The code you need to drop in your ~/.irbrc is located near the bottom of his post, but if you’re using qri (like me) you’ll want to change one line in the pasted code:

desc = `fri '#{candidate}'`
desc = `qri '#{candidate}'`

Then you can prepend “ri_” to any method and get its documentation, right there in the console! Try it out:

>> {}.ri_inject

Tab completion, a life saver for long methods, should also work.

But can we look up, say, ActiveRecord methods on a model in our Rails application?

>> Message.ri_update_all
----------------------------------------- ActiveRecord::Base::update_all
     ActiveRecord::Base::update_all(updates, conditions = nil, options 
     = {})
------------------------------------------------------------------------
     Updates all records with details given if they match a set of 
     conditions supplied, limits and order can also be supplied.
     ...

This not only works, but works awesomely. It is the greatest addition to my .irbrc in a long time.