Forum: Ruby why is require 'rubygems' sometimes needed?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A74096209c78118ff69d50a51129d697?d=identicon&s=25 Jason Lillywhite (jlillywh)
on 2009-06-04 07:46
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!
9239e324751b8db551789e5e16689438?d=identicon&s=25 Hunt Jon (Guest)
on 2009-06-04 08:01
(Received via mailing list)
If it's not a gem, you don't need require 'rubygems'.
If it comes with ruby like yaml, you don't need 'rubygems'.
Ef0db53920b243d6758c2f6b1306df0d?d=identicon&s=25 Steve Ross (cwd)
on 2009-06-04 08:12
(Received via mailing list)
On Jun 3, 2009, at 10:46 PM, Jason Lillywhite 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.
A74096209c78118ff69d50a51129d697?d=identicon&s=25 Jason Lillywhite (jlillywh)
on 2009-06-04 08:39
Yes, that makes sense. Thanks for the explanation.
6e366eb5a71be2bad7f383d42aeb4788?d=identicon&s=25 Justin Collins (Guest)
on 2009-06-04 08:43
(Received via mailing list)
Jason Lillywhite 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
Ee2809522b2e56d0d6b656486bc5e0db?d=identicon&s=25 Markus Fischer (Guest)
on 2009-06-04 23:21
(Received via mailing list)
Hi,

Justin Collins 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
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2009-06-04 23:56
(Received via mailing list)
On Jun 4, 2009, at 14:20, Markus Fischer wrote:
> Justin Collins 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.
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2009-06-05 00:53
> 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).
Ba91caa4b1fd66471e4180151b379dd7?d=identicon&s=25 Jörg W Mittag (Guest)
on 2009-06-05 01:20
(Received via mailing list)
Markus Fischer wrote:
> Justin Collins 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
This topic is locked and can not be replied to.