Is there any relevant difference between require and require_gem?

L

Lars Olsson

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
 
A

Austin Ziegler

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'

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
 
J

Jim Weirich

lasso said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top