Rake has become a critical component of my development toolchain. So much so that I have developed extensions, such as Chrysalis, to assist with common aspects of the build cycle.
However, due to time constraints, Chrysalis does not support the latest version of Rake (currently 0.8.3). The 0.8 branch reworked some of the internals, and Chrysalis was developed against 0.7.3. As such, I often need to quickly switch between multiple versions of Rake.
Thankfully, the executable RubyGems places in the bin directory makes this easy. I had long been curious how this works, so I took the time to figure it out.
The executable scripts generated by RubyGems, in this case for Rake, look like the following:
require 'rubygems'
version = ">= 0"
if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then
version = $1
ARGV.shift
end
gem 'rake', version
load 'rake'
There is a bit of regular expression magic that works on the first argument to the command, accessed through ARGV.first. The =~ is the positive match operator for regular expressions. If a match is found, a portion of it will be stored in the global variable $1.
The goal of the RubyGems-generated script is to match a user-supplied version number, without interfering with any other arguments the actual script may expect. In order to do so, the version argument is surrounded by underscores. This is enforced by using the ^ and $ as achors for the beginning and end of the string, respectively.
The result of all this is a simple way to switch between versions easily on the command line.
$ rake _0.7.3_ --version
$ rake _0.8.3_ --version
