Forum: Ruby require_gem vs. gem

C227f9f1d21b776491c711cd5c89533a?d=identicon&s=25 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.
Dcef09335767135acb26ecfebbc65198?d=identicon&s=25 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
96931bfe0c2948f47a98e15ae52e5637?d=identicon&s=25 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'
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 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 !
C227f9f1d21b776491c711cd5c89533a?d=identicon&s=25 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".
Dcef09335767135acb26ecfebbc65198?d=identicon&s=25 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!
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 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).
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 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/
10d4acbfdaccb4eee687a428ca00a5d8?d=identicon&s=25 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
C227f9f1d21b776491c711cd5c89533a?d=identicon&s=25 Dennis Crissman (raughan)
on 2007-05-24 14:39
Thats great guys, thank you for the explanation.
9f821349470fc26abc6922b0816710c6?d=identicon&s=25 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
55eddb03eeaf352cd86f04eaafe12131?d=identicon&s=25 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

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.