Forum: Ruby What is the point of require_gem in Ruby 1.8.2?

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.
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-03-23 18:51
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
10d4acbfdaccb4eee687a428ca00a5d8?d=identicon&s=25 Jim Weirich (weirich)
on 2006-03-23 19:45
Wes Gamble wrote:
> 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.

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 Weirich
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-03-23 21:26
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 Weirich wrote:
> Wes Gamble wrote:
>> 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.
>
> 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 Weirich
10d4acbfdaccb4eee687a428ca00a5d8?d=identicon&s=25 Jim Weirich (weirich)
on 2006-03-24 00:23
Wes Gamble 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 Weirich

[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.
This topic is locked and can not be replied to.