require_gem vs. gem

D

Dennis Crissman

I am confused, can somebody please explain to me the difference between
these two calls?

I tried some variations below, and the only one that worked was the
obsolete require_gem call.

----------

user@vm0_gentoo /my/path $ irb -r rubygems
irb(main):001:0> require 'activerecord'
LoadError: no such file to load -- activerecord
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`gem_original_require'
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
from (irb):1
irb(main):002:0> exit

----------

user@vm0_gentoo /my/path $ irb -r rubygems
irb(main):001:0> gem 'activerecord'
=> true
irb(main):002:0> ActiveRecord
NameError: uninitialized constant ActiveRecord
from (irb):2
irb(main):003:0> exit

----------

user@vm0_gentoo /my/path $ irb -r rubygems
irb(main):001:0> gem 'activerecord','=1.14.4'
=> true
irb(main):002:0> ActiveRecord
NameError: uninitialized constant ActiveRecord
from (irb):2
irb(main):003:0> exit

----------

user@vm0_gentoo /my/path $ irb -r rubygems
irb(main):001:0> require_gem 'activerecord'
(irb):1:Warning: require_gem is obsolete. Use gem instead.
=> true
irb(main):002:0> ActiveRecord
=> ActiveRecord
 
E

Enrique Comba Riepenhausen

I am confused, can somebody please explain to me the difference
between
these two calls?

I tried some variations below, and the only one that worked was the
obsolete require_gem call.

----------

user@vm0_gentoo /my/path $ irb -r rubygems
irb(main):001:0> require 'activerecord'
LoadError: no such file to load -- activerecord
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`gem_original_require'
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
from (irb):1
irb(main):002:0> exit

----------

user@vm0_gentoo /my/path $ irb -r rubygems
irb(main):001:0> gem 'activerecord'
=> true
irb(main):002:0> ActiveRecord
NameError: uninitialized constant ActiveRecord
from (irb):2
irb(main):003:0> exit

----------

user@vm0_gentoo /my/path $ irb -r rubygems
irb(main):001:0> gem 'activerecord','=1.14.4'
=> true
irb(main):002:0> ActiveRecord
NameError: uninitialized constant ActiveRecord
from (irb):2
irb(main):003:0> exit

----------

user@vm0_gentoo /my/path $ irb -r rubygems
irb(main):001:0> require_gem 'activerecord'
(irb):1:Warning: require_gem is obsolete. Use gem instead.
=> true
irb(main):002:0> ActiveRecord
=> ActiveRecord

You should use the following now:

require 'rubygems'
require 'activerecord'

That should solve your problem...

Cheers,

Enrique
 
C

Chris Carter

I am confused, can somebody please explain to me the difference between
these two calls?

I tried some variations below, and the only one that worked was the
obsolete require_gem call.

----------

user@vm0_gentoo /my/path $ irb -r rubygems
irb(main):001:0> require 'activerecord'
LoadError: no such file to load -- activerecord
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`gem_original_require'
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
from (irb):1
irb(main):002:0> exit

----------

user@vm0_gentoo /my/path $ irb -r rubygems
irb(main):001:0> gem 'activerecord'
=> true
irb(main):002:0> ActiveRecord
NameError: uninitialized constant ActiveRecord
from (irb):2
irb(main):003:0> exit

----------

user@vm0_gentoo /my/path $ irb -r rubygems
irb(main):001:0> gem 'activerecord','=1.14.4'
=> true
irb(main):002:0> ActiveRecord
NameError: uninitialized constant ActiveRecord
from (irb):2
irb(main):003:0> exit

----------

user@vm0_gentoo /my/path $ irb -r rubygems
irb(main):001:0> require_gem 'activerecord'
(irb):1:Warning: require_gem is obsolete. Use gem instead.
=> true
irb(main):002:0> ActiveRecord
=> ActiveRecord

try require 'active_record'
 
D

Dennis Crissman

François Beausoleil said:
The #gem call activates the gem, and the require is the regular
require to load a library.

What is the difference between an "active" gem and a gem that has been
"loaded".
 
E

Enrique Comba Riepenhausen

Apparently, it didn't solve Enrique's problem.
Enrique, I believe the way to do it now is:

require "rubygems"
gem "activerecord", "=3D 0.14.4"
require "activerecord"

The #gem call activates the gem, and the require is the regular
require to load a library.

Hope that helps !
--=20
Fran=E7ois Beausoleil
http://blog.teksol.info/
http://piston.rubyforge.org/

Well, i did not have the problem in the first place :p

And I use it like that in my programs and it works... (?)

cheers!=
 
L

Logan Capaldo

I am confused, can somebody please explain to me the difference between
these two calls?

I tried some variations below, and the only one that worked was the
obsolete require_gem call.
require 'active_record'

The developers of ActiveRecord made the (IMO unfortunate) choice of
having the name of the file (active_record.rb) be different from the
name of the gem (activerecord).
 
B

Brady Behrman

unsubscribe

Brady Behrman
(757) 271-8293
(e-mail address removed)
http://www.VBWebSites.net


-----Original Message-----
From: Chris Carter [mailto:[email protected]]
Sent: Wednesday, May 23, 2007 12:08 PM
To: ruby-talk ML
Subject: Re: require_gem vs. gem

I am confused, can somebody please explain to me the difference between
these two calls?

I tried some variations below, and the only one that worked was the
obsolete require_gem call.

----------

user@vm0_gentoo /my/path $ irb -r rubygems
irb(main):001:0> require 'activerecord'
LoadError: no such file to load -- activerecord
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`gem_original_require'
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
from (irb):1
irb(main):002:0> exit

----------

user@vm0_gentoo /my/path $ irb -r rubygems
irb(main):001:0> gem 'activerecord'
=> true
irb(main):002:0> ActiveRecord
NameError: uninitialized constant ActiveRecord
from (irb):2
irb(main):003:0> exit

----------

user@vm0_gentoo /my/path $ irb -r rubygems
irb(main):001:0> gem 'activerecord','=1.14.4'
=> true
irb(main):002:0> ActiveRecord
NameError: uninitialized constant ActiveRecord
from (irb):2
irb(main):003:0> exit

----------

user@vm0_gentoo /my/path $ irb -r rubygems
irb(main):001:0> require_gem 'activerecord'
(irb):1:Warning: require_gem is obsolete. Use gem instead.
=> true
irb(main):002:0> ActiveRecord
=> ActiveRecord

try require 'active_record'
 
R

Rick DeNatale

What is the difference between an "active" gem and a gem that has been
"loaded".

"active" gem is informal. What the gem directive really does is to
set up the load path before the require. Normally it's not needed
unless you want to specify a version of the gem other than the
default.

$ qri gem
------------------------------------------------------------- Kernel#gem
gem(gem_name, *version_requirements)
------------------------------------------------------------------------
Adds a Ruby Gem to the $LOAD_PATH. Before a Gem is loaded, its
required Gems are loaded. If the version information is omitted,
the highest version Gem of the supplied name is loaded. If a Gem
is not found that meets the version requirement and/or a required
Gem is not found, a Gem::LoadError is raised. More information on
version requirements can be found in the Gem::Version
documentation.

The gem directive should be executed before any require statements
(otherwise rubygems might select a conflicting library version).

You can define the environment variable GEM_SKIP as a way to not
load specified gems. you might do this to test out changes that
haven't been intsalled yet. Example:

GEM_SKIP=3DlibA:libB ruby-I../libA -I../libB ./mycode.rb

gem: [String or Gem::Dependency] The gem name or
dependency instance.

version_requirement: [default=3D">=3D 0.0.0"] The version requirement.

return: [Boolean] true if the Gem is loaded,
otherwise false.

raises: [Gem::LoadError] if Gem cannot be found, is
listed in GEM_SKIP, or version requirement
not met.

--=20
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

IPMS/USA Region 12 Coordinator
http://ipmsr12.denhaven2.com/

Visit the Project Mercury Wiki Site
http://www.mercuryspacecraft.com/
 
J

Jim Weirich

Dennis said:
What is the difference between an "active" gem and a gem that has been
"loaded".

In short:
Gems are activated (i.e. selected and made available).
Files are loaded.

The long description:

The 'require' command loads files by searching for the file name in a
list of directories (called the load path). If a file is in one of the
directories in the load path, it is loaded into the Ruby program.

By default, the files in a Gem are not in the load path used by Ruby.
RubyGems extends the standard 'require' command so that if a file is not
found, the latest gem containing that file will activated (see below).

When a gem is activated, the load path used by Ruby is adjusted so that
the files in the gem will be in the load path. Then Ruby can find any
file in that gem.

The only time you need to use a 'gem' command in your source code is
when you want to use a particular version of a gem. Omitting the gem
command just means that you get the latest version of whatever gem it is
found in.

Examples:

# Use the latest version of the activerecord gem:
require 'active_record'

# Use the version 1.14.4 of activerecord
gem 'activerecord', '=1.14.4'
require 'active_record'

Question: Why is require_gem obsolete?

Answer: Because it did two things that should be separate.

require_gem would activate a gem (i.e. put its files in the load_path)
and then autorequire the main gem file.

Activating a gem is something that should be done once in a rather
centrally organized location in the code. Requiring files is something
that should be done in every file that uses code from the file being
requires. 'require_gem' intermingled those two operations and
encouraged bad coding practices.

Does that help?

-- Jim Weirich
 
M

Mark Gallop

Jim said:
In short:
Gems are activated (i.e. selected and made available).
Files are loaded.

I was just wondering the same as the original poster. These explanations
were really helpful. Many thanks.

Mark
 
A

Abder-Rahman Ali

François Beausoleil said:
Hi,



Apparently, it didn't solve Enrique's problem.

Enrique, I believe the way to do it now is:

require "rubygems"
gem "activerecord", "= 0.14.4"
require "activerecord"

The #gem call activates the gem, and the require is the regular
require to load a library.

Hope that helps !

Shouldn't:

require "activerecord"

be:

require "active_record"

?
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top