Forum: Ruby Is there any relevant difference between require and require

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.
F9bd1e8a646ed947d4ed701cebffa9e2?d=identicon&s=25 lasso (Guest)
on 2005-12-02 08:15
(Received via mailing list)
Hi all!

When using Rubygems, is there any *relevant* difference between using
require and require_gem (aside from the versioning possibilities)?
According to the docs require searches the standard library first. Is
this true for require_gem too, or does that method only search the gem
repository? Is is faster?

Example:
require 'rubygems'

# Is any of the two methods below "better" in any way?
require 'sqlite3'

# Or

require_gem 'sqlite3-ruby'


Kindly

/Lars
10d4acbfdaccb4eee687a428ca00a5d8?d=identicon&s=25 Jim Weirich (weirich)
on 2005-12-02 15:29
lasso wrote:
> Hi all!
>
> When using Rubygems, is there any *relevant* difference between using
> require and require_gem (aside from the versioning possibilities)?
> According to the docs require searches the standard library first. Is
> this true for require_gem too, or does that method only search the gem
> repository? Is is faster?

There is a fundamental difference between require and require_gem:

(1) require specifies a file name and loads that file into your ruby
session.

(2) require_gem specifies a gem name and puts the files in that gem on
the search path so that later requires will be able to find the gems
file (we call this process "gem activation").  Sometimes require_gem
will auto-require the "main" file of a gem (if the author if the gem
specified an main file).

RubyGems is smart enough that it can generally figure out what gem needs
to be activated via normal require statements, so the require_gem is
almost never needed.  In fact, I would recommend only using the
require_gem statement if you need a particular version a gem package (by
default, RubyGems will select the latest version for you).

I'll repeat:  Never use "require_gem" without a version specification.
Prefer to use a plain "require" instead.

Future versions of RubyGems will rename "require_gem" (probably to
"activate_gem") and have it drop the auto-require step.  This should
lessen the confusion between the normal require and RubyGems activation
process.

-- Jim Weirich
31ab75f7ddda241830659630746cdd3a?d=identicon&s=25 halostatue (Guest)
on 2005-12-02 15:31
(Received via mailing list)
On 12/2/05, Lars Olsson <lasso@lassoweb.nu> wrote:
> require 'sqlite3'
>
> # Or
>
> require_gem 'sqlite3-ruby'

Yes. require_gem, at least for some gems, and *definitely* for future
versions of PDF::Writer and other packages that I write will only
require the gem. It will not autorequire the core functionality as it
does now.

-austin
This topic is locked and can not be replied to.