Markus F. wrote:
Justin C. wrote:
Just as a note (as your question has already been answered), requiring
rubygems is no longer needed in Ruby 1.9.
How so? I.e., what has been changed that the libraries are automatically
found?
Nothing. You still need the RubyGems library for that. But, in 1.9
RubyGems has been merged into Ruby and is always available
automatically.
Because currently, with 1.8.7, I think I had some troubles getting my
code portable.
On my home machine, I’ve installed rubyzip through gem and thus I
required rubygems. On another machine, rubyzip was installed with a
distribution specific mechanism and there were no rubygems at all installed.
How would I tackle this problem? Would I need rescue the rubygem require
statement? Are there elegant solutions for this?
In general, you should never, ever require 'rubygems’ – at least not
in code you expect others to use. How the system administrator chooses
to install packages is her decision. (I personally would rather use
dpkg/APT, RPM/YUM, RPM/urpmi, RPM/YaST2, Portage, pkgsrc or just plain
tarballs or maybe .jar files instead of RubyGems.) And if she chooses
to use RubyGems, then it is her job to make sure the RubyGems library
gets loaded, by whatever means are appropriate (the RUBYOPT
environment variable, or maybe even a patch to the Ruby interpreter,
for example).
You, in your code should never need to require RubyGems: either the
sysadmin doesn’t use RubyGems or she has already loaded it for you.
And, yes, I have been guilty of that, too. All my code had very
elaborate, and very defensive code blocks like this (in fact, way too
much of my code still looks like this), but I’m getting rid of them:
begin require ‘spec’; rescue LoadError
begin require ‘rubygems’; rescue LoadError
else begin gem ‘rspec’, ‘~> 1.1.4’; rescue Gem::LoadError; end end
ensure begin require ‘spec’; rescue LoadError
warn ‘Please make sure to install RSpec’; raise end end
shudder
This covers pretty much every conceivable combination: RubyGems is not
installed, and RSpec isn’t either. RubyGems is not installed, but
RSpec is (from the tarballs). RubyGems is installed, but RSpec
isn’t. Gems is installed, and RSpec is installed as a Gem. Gems is
installed, but RSpec is installed from source.
It’s also ugly as hell.
jwm