Doxavore

Fast, developer-friendly Rails console

December 11th, 2010

A fresh Rails application is pretty fast. But if you’re like me, you’ve found a few little gems that help you when you’re in the console. Personally, I use:

  • awesome_print: Inspect objects without hurting your eyes.
  • hirb: View your ActiveModel objects in table format when possible.
  • looksee: Examine where an object gets its methods.

So let’s say we’ve discovered something we want available in our Rails console. For it to be available in your Bundler-only Rails environment, it will need to be listed in the Gemfile:

group :development do
  gem 'awesome_print'
  gem 'hirb'
  gem 'looksee'
end

Now you can launch a new console to ap and lp until your heart is content. Wunderbar! Until we try to load our application in a browser. In a small app with a number of popular plugins, it can normally take about 500ms on a modest machine to load a simple page due to the reloading Rails does to get your latest code. When we add these, that time more than doubles. Ouch!

So what gives? Our logs show the time hasn’t changed, but Firebug shows the times are closer to 1300ms now. A little digging reveals that awesome_print is the primary culprit, and removing it boosts my load times back up almost to what they used to be. But I love awesome_print and don’t want to lose it! After investigating rails-dev-boost (which improved load times tremendously but had a few issues where it wouldn’t find a file has been updated), I realized since they’re in our Gemfile, Rails will require them when refreshing the environment. We can disable that trivially:

group :development do
  gem 'awesome_print', :require => false
  gem 'hirb', :require => false
  gem 'looksee', :require => false
end

To avoid having to require them every time we load our console, just add it to your .irbrc:

begin
  require "ap"
  IRB::Irb.class_eval do
    def output_value
      ap @context.last_value
    end
  end
rescue LoadError
end

begin
  require 'hirb'
  require 'hirb/import_object'
  Hirb.enable
  extend Hirb::Console
rescue LoadError  
end

begin
  require 'looksee/shortcuts'
rescue LoadError  
end

And voila! Our development-mode page load times don’t suffer and we get to leverage some awesome helpers in the console.

By the way, thanks for reading my inaugural blog post!