.irbrc and testing rails

As part of the Rails Hackfest 2007 contest for Rails contributors, I have been testing Rails quite a bit. Although I try to write failing tests when possible, sometimes it's just easier to fire up irb to try something.

If you often want to load a gem in irb, such as ActiveRecord or ActiveSupport, make it easier by defining methods like these in your ~/.irbrc file:

require 'rubygems'

def ar
  gem 'activerecord'
  require 'active_record'
  ActiveRecord::Base.establish_connection({:adapter => 'mysql', :database => 'activerecord_unittest'})
end

def as
  gem 'activesupport'
  require 'active_support'
end

Now, in irb:
$ irb
irb(main):001:0> as
=> true
irb(main):002:0> "ruby".pluralize
=> "rubies"
irb(main):003:0> ar
=> #"mysql", :database=>"activerecord_unittest"}, @adapter_method="mysql_connection">
irb(main):004:0> ActiveRecord::Base.connection.tables.size
=> 45
irb(main):005:0>

If you're not on RubyGems 0.9.1 yet (gem --version to check), 'require_gem' is deprecated, which is why I'm using 'gem'. To update:

sudo gem update --system 
gem pristine --all

Typing a few lines of code to load ActiveSupport may not seem like much, but after doing it many times per day, typing 'as' is much nicer. Take advantage of the .irbrc file to save repetitive keystrokes every time you open irb.