RUBYOPT - How it works
The RUBYOPT environment variable is a handy way to specify the options that will be pased to ruby without having to type them in all the time.
According to the ruby source code you can customize your ruby interpreter by specifying any of the following arguments in RUBYOPT: I, d, v, w, W, r, or K
For example to always require rubygems when you run ruby simply add the following to your .profile or .bashrc.
export RUBYOPT="-r rubygems"
But wait there’s more! According to the rubygems documentation you can simply say
RUBYOPT='rubygems'
How does that work?
Well this trick depends on a couple of things. First RUBYOPT does not actually require you to put a dash in front of your arguments. This kind of makes sense when you think about it since you have already declared the value of RUBYOPT to be options to the ruby interpreter by virtue of the fact that they are in a special environment variable. However, on the command line dashes are required to distinguish between arguments and files to run. So what does not requiring dashes leave us? Well you could expand
RUBYOPT='rubygems'
into
RUBYOPT='-r ubygems'
This brings us to the second part of the trick. Rubygems ships with a file named ubygems.rb that has this line require 'rubygems'.
All of this adds up to some ‘command line syntactic sugar’ allowing you to simply specify RUBYOPT='rubygems' to require rubygems by default in all ruby programs.
This post was inspired by an error I received yesterday; here it is for the benefit of those googling:
no such file to load -- ubygem (LoadError).
The problem is that RUBYOPT was set to ‘rubygem’ when it should have been set to ‘rubygems’, or ‘-r rubygems’, or, ‘rrubygems’, or ‘-r ubygems’… you get the idea.
Published: 20 Nov, 2008