Why is require 'rubygems' sometimes needed?


#1

I noticed that some libraries (such as wx or gnuplot) need to have

require ‘rubygems’

before the require of the actual library of interest. But some other
libraries (such as gsl) do not require this. Can someone explain why
this is the case?

Thank you!


#2

If it’s not a gem, you don’t need require ‘rubygems’.
If it comes with ruby like yaml, you don’t need ‘rubygems’.


#3

On Jun 3, 2009, at 10:46 PM, Jason L. wrote:

I noticed that some libraries (such as wx or gnuplot) need to have

require ‘rubygems’

before the require of the actual library of interest. But some other
libraries (such as gsl) do not require this. Can someone explain why
this is the case?

Thank you!

The most obvious reason to include rubygems is if you need some of the
facilities provided by … uh … rubygems. But that’s not the reason
most code requires it. It turns out rubygems.rb is special because it
is installed in /path/to/lib/ruby/site_ruby and its location in the
filesystem is known to the interpreter. Where your actual gems are
stored is normally in /path/to/lib/ruby/gems/maj.min/gems, but that’s
not mandatory. Say Ruby is installed in a directory to which you don’t
have write access. You would need to install your gems in a place
where you do have write access, so you see that can’t be known by the
interpreter at build time.

At the point where you require (and execute, as a side effect)
rubygems.rb, the GEM_HOME environment variable is used to puzzle out
where gems might be. Some other options may also be explored. These
are added to the load path so that requires on subsequent gems
actually find the gems.

To answer your question more directly. If you need to require another
gem, then you should require rubygems. If not, then you can make your
code one line shorter.

Hope this helps explain.


#4

Yes, that makes sense. Thanks for the explanation.


#5

Jason L. wrote:

I noticed that some libraries (such as wx or gnuplot) need to have

require ‘rubygems’

before the require of the actual library of interest. But some other
libraries (such as gsl) do not require this. Can someone explain why
this is the case?

Thank you!

Just as a note (as your question has already been answered), requiring
rubygems is no longer needed in Ruby 1.9.

-Justin


#6

Hi,

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?

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?

thanks,

  • Markus

#7

On Jun 4, 2009, at 14:20, 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?

RubyGems is built-in.

Because currently, with 1.8.7, I think I had some troubles getting my
code portable.

require ‘rubygems’ returns false in 1.9, indicating that RubyGems is
already loaded. If you want code that runs on both, keep require
‘rubygems’ around.

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?

Since you probably needed other software that came from RubyGems,
there’s no reason to rescue the require on RubyGems.


#8

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


#9

If not, then you can make your code one line shorter.

As trivial as it may sound, but this extra line in my code was one
reason I did not use rubygems. I really did not want to specifically
require rubygems, just to require another library.

Let’s go back in time, and think about a ruby version which did not have
yaml inbuilt, but rubygems existed already (yes, yes, this is
speculative…)

One would always have to do:

require ‘rubygems’
require ‘yaml’

Before using yaml datasets. To me this means a bit of extra work, and I
found it
much easier to just omit the first line.

I am glad that ruby 1.9.x changed this behaviour (for the better).