rake console: a custom irb instance for your Ruby GEM

Are you a Ruby GEM author? Are you tired to open irb sessions and load your library to debug it? Here's the solution: create a rake task to initialize a new irb session and preload your library.

The task is really straightforward. You can find an example in my Ruby Whois library.

Grab the following script and paste it into your library Rakefile.

desc "Open an irb session preloaded with this library"
task :console do
  sh "irb -rubygems -I lib -r whois.rb"
end

Then replace whois.rb with the main file that represents your GEM.

Now you can open a new irb instance and get your library automatically mixed into your irb session.

$ rake console
(in /Users/weppos/Code/whois)
irb -rubygems -I lib -I test -r whois.rb
>> Whois
=> Whois

You no longer need to launch irb and include you library all the time. Stop wasting your time with the following irb sessions.

$ cd lib
$ irb
>> require 'whois'
=> true
>> Whois
=> Whois

By default, this task appends the lib folder to the irb session $LOAD_PATH. If you need to append multiple paths, call -I once for any additional folder.

desc "Open an irb session preloaded with this library"
task :console do
  sh "irb -rubygems -I lib -I  extra -r whois.rb"
end

To learn more about irb parameters, open a shell and enter the following command

$ irb --help