I’m trying to use rake in my ruby script…(Ruby 1.8.6, JRuby 1.6.5)
Downloaded rake using gem install --remote rake, looks ok on install…
Fetching: rake-0.9.2.2.gem (100%)
Successfully installed rake-0.9.2.2
1 gem installed
I’ve got a simple ruby script which works fine, but when I import rake to using any of the following requires, it starts complaining….
require 'rake'
LoadError: no such file to load -- rake
or
require '/lib/rake'
LoadError: no such file to load -- lib/rake
After some searching, I found that adding require 'rubygems' just before rakefixes the issue….
require 'rubygems'
require 'rake'
Even though it’s working, I’ve got some questions…
-
The gem spec on
rakeshows therequire_pathaslib, so why
doesn’trequire '/lib/rake'work? Am I misunderstanding the significance ofrequire_path? -
Why is it necessary to place
require 'rubygems'beforerequire
'rake'
Yes, you are misunderstanding the significance. The
require_pathsin the specification is an array of subdirectories of that gem’s installation directory that should be searched for files belonging to the gem.To find out where rake really is, try this:
You’ll see that it is actually installed somewhere completely unrelated to
/lib; on my system, it’s under/var/lib/gems. Ruby gems, in general, live in their own directory structure; the only file in the standard Ruby include path ($:) isrubygemsitself, which you used to have to explicitlyrequirein order to make any actual gems visible. (Since Ruby 1.9, that has not been necessary.)Gems are more complex than just libraries to load; you can have multiple versions of the same gem installed, and specify which one you want at a time, and do other things that wouldn’t work if the gems were just dumped into the standard Ruby include path.