rake console: a custom irb instance for your Ruby GEM

September 28th, 2009 at 11:50 pm • permalink2 comments

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
  1. Ruby Whois 0.8.0
  2. Capistrano: Executing a command as root without using sudo
  3. Ruby has a new WHOIS library
  4. Ruby Whois 0.8.1
  5. Running Capistrano with Passenger (mod_rails)

Filed in Programming • Tags: , , , ,

Comments

[...] rake console: a custom irb instance for your Ruby GEM [...]

andhapp says:

Cool. I know you can do that with Jekyll but never had time to explore it. Thanks for the article.

Add a Comment




Follow Me
    Random Quote