a serious blog.

Project specific .irbrc

Posted in tech by Seth Rasmussen on February 11, 2010

Users of Ruby’s interactive, REPL-tastic program irb should know about the ability to bootstrap your irb environment using ~/.irbrc. If such a file exists, Ruby will load it before giving you a prompt to start entering lines of code. You can put whatever Ruby code you want in there!

Here is my ~/.irbrc file, part of my “dotfiles” project on GitHub: http://github.com/greatseth/dotfiles/blob/master/irbrc

There are a few handy code snippets in there. Here is one of my favorites:

# Project-specific .irbrc
if Dir.pwd != File.expand_path("~")
  local_irbrc = File.expand_path '.irbrc'
  if File.exist? local_irbrc
    puts "Loading #{local_irbrc}"
    load local_irbrc
  end
end

This says: If we aren’t in ~/, we look for .irbrc in the current working directory. If we find it, we load it!

That snippet enables an intuitive and really simple hook for project-specific irb bootstrapping. Here’s an example from my latest labor of love Walter:

require 'grit'
GRIT_EXTENSIONS_PATH = File.expand_path './lib/walter/grit_extensions.rb'
def reload_grit_extensions!; load GRIT_EXTENSIONS_PATH; end
reload_grit_extensions!

No big whoop, but it’s awesome to have stuff you do all the time in your irb hackings utomatically setup for you.

Advertisement
Tagged with: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.