What is the point of require_gem in Ruby 1.8.2?

I just went through this mess over the last few hours…

http://www.ruby-forum.com/topic/59185

If I do:

require_gem ‘gemname’, ‘>= version’

I discovered that require_gem doesn’t actually do the “require” call on
the 1st argument.

So what’s the point of require_gem then?

It did manage to confuse the heck out of me, so I guess it does
something :)…

Wes

Wes G. wrote:

I just went through this mess over the last few hours…

Problem seeing classes in rubygem - Ruby - Ruby-Forum

If I do:

require_gem ‘gemname’, ‘>= version’

I discovered that require_gem doesn’t actually do the “require” call on
the 1st argument.

That’s because the first argument is the name of the Gem to be
activated, not the name of a file to be required.

However, many gems have the same name as their main require file, so
there is a lot of confusion. Also some gems have an ‘autorequire’ flag
set that /will/ do a require on the main file.

And to futher the confussion, the command name ‘require_gem’ was poorly
selected. Future versions of RubyGems will provide a better named
command for this operation.

So what’s the point of require_gem then?

If you have multiple versions of a gem installed, the require_gem
command specifies what version of the gem you are willing to use. If
you don’t care what version will be used, and just want the latest
version of whatever is installed, then there is no reason to even bother
with the require_gem command.


– Jim W.

Jim,

So I think the question still stands. Is require_gem only do the
version checking?

What does it mean to “activate” a Gem?

Wes

Jim W. wrote:

Wes G. wrote:

I just went through this mess over the last few hours…

Problem seeing classes in rubygem - Ruby - Ruby-Forum

If I do:

require_gem ‘gemname’, ‘>= version’

I discovered that require_gem doesn’t actually do the “require” call on
the 1st argument.

That’s because the first argument is the name of the Gem to be
activated, not the name of a file to be required.

However, many gems have the same name as their main require file, so
there is a lot of confusion. Also some gems have an ‘autorequire’ flag
set that /will/ do a require on the main file.

And to futher the confussion, the command name ‘require_gem’ was poorly
selected. Future versions of RubyGems will provide a better named
command for this operation.

So what’s the point of require_gem then?

If you have multiple versions of a gem installed, the require_gem
command specifies what version of the gem you are willing to use. If
you don’t care what version will be used, and just want the latest
version of whatever is installed, then there is no reason to even bother
with the require_gem command.


– Jim W.

Wes G. wrote:

Jim,

So I think the question still stands. Is require_gem only do the
version checking?

The require_gem does two basic functions:

(1) Activates a gem with an appropriate version number (see below for
more on “activate”)

(2) Auto requires the autorequire file if the gem has specified one.

After much experience with gems, the RubyGems team has come to the
conclusion that the autorequire step causes more trouble than its worth.
The require_gem command will be probably be deprecated in the future and
be replaced with a command that only does the activation step.
(‘activate_gem’ will probably be then name of this new command, although
no promises there).

Require_gem is only needed if you wish to assert which versions of a gem
you are willing to accept. If you don’t care, it is better to not use
it.

What does it mean to “activate” a Gem?

Gems are not stored in a place where Ruby can normally find them. To
activate a gem means to manipulate Ruby’s search path so that the gem
may be found by a require statement. Only one version of any given gem
can be activated at a time.

Today, Gems monitors all require requests given to Ruby. If a require
fails to load a file, then Gems will check its repository to see if
there is a file that satisfies the require statement. If so, RubyGems
will activate the latest version of the gem that provides that file and
will retry the require.

Since RubyGems now automatically handles normal require statements
semi-intelligently, the autorequire feature of the original require_gem
is more of a hinderence than a help[1].


– Jim W.

[1] For example, if a gem has an autorequire file, then that autorequire
file will be loaded by the activation step. If a user does a normal
“require ‘xyz’” that triggers an activation, when the require is retried
the file will be already ‘autorequired’. There’s is no harm in that
except that the result of the user’s require statement returns false
(since the file will autoloaded durning activate), and returning false
confuses a lot of people. I hope this footnote didn’t ramble too much.