Forum: Ruby require_gem vs. gem

Posted by Dennis Crissman (raughan)
on 2007-05-23 17:29
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

----------

Any help you could offer would be appreciated.
Posted by Enrique Comba Riepenhausen (Guest)
on 2007-05-23 18:03
(Received via mailing list)
On 23 May 2007, at 17:29, Dennis Crissman wrote:

> irb(main):001:0> require 'activerecord'
>
> user@vm0_gentoo /my/path $ irb -r rubygems
> irb(main):001:0> require_gem 'activerecord'
> Posted via http://www.ruby-forum.com/.
>

You should use the following now:

require 'rubygems'
require 'activerecord'

That should solve your problem...

Cheers,

Enrique
Posted by Chris Carter (cdcarter)
on 2007-05-23 18:09
(Received via mailing list)
On 5/23/07, Dennis Crissman <dcrissman@perimeterusa.com> wrote:
> LoadError: no such file to load -- activerecord
> user@vm0_gentoo /my/path $ irb -r rubygems
> irb(main):001:0> gem 'activerecord','=1.14.4'
> (irb):1:Warning: require_gem is obsolete.  Use gem instead.
>
>

try require 'active_record'
Posted by François Beausoleil (fbeausoleil)
on 2007-05-23 18:13
(Received via mailing list)
Hi,

2007/5/23, Enrique Comba Riepenhausen <ecomba@mac.com>:
> > irb(main):002:0> exit
>
> You should use the following now:
>
> require 'rubygems'
> require 'activerecord'

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 !
Posted by Dennis Crissman (raughan)
on 2007-05-23 18:31
François Beausoleil wrote:
> 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".


Posted by Enrique Comba Riepenhausen (Guest)
on 2007-05-23 18:46
(Received via mailing list)
>
> Hope that helps !
> -- 
> François 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!
Posted by Logan Capaldo (Guest)
on 2007-05-23 18:48
(Received via mailing list)
On Thu, May 24, 2007 at 12:29:58AM +0900, Dennis Crissman wrote:
> 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).
Posted by Rick Denatale (rdenatale)
on 2007-05-23 19:18
(Received via mailing list)
On 5/23/07, Dennis Crissman <dcrissman@perimeterusa.com> wrote:
> François Beausoleil wrote:
> > 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".

"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=libA:libB ruby-I../libA -I../libB ./mycode.rb

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

     version_requirement: [default=">= 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.

--
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/
Posted by Jim Weirich (weirich)
on 2007-05-23 23:59
Dennis Crissman wrote:
> François Beausoleil wrote:
>> 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".

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
Posted by Dennis Crissman (raughan)
on 2007-05-24 14:39
Thats great guys, thank you for the explanation.
Posted by Mark Gallop (markg)
on 2007-06-14 04:25
Jim Weirich wrote:
> Dennis Crissman wrote:
>> François Beausoleil wrote:
>>> 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".
> 
> 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
Posted by SW Engineer (swengineer)
on 2010-09-07 17:23
François Beausoleil wrote:
> Hi,
> 
> 2007/5/23, Enrique Comba Riepenhausen <ecomba@mac.com>:
>> > irb(main):002:0> exit
>>
>> You should use the following now:
>>
>> require 'rubygems'
>> require 'activerecord'
> 
> 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"

?

Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.