[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

require_gem vs. gem

Dennis Crissman

5/23/2007 3:30:00 PM

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 via http://www.ruby-....

11 Answers

Enrique Comba Riepenhausen

5/23/2007 4:01:00 PM

0


On 23 May 2007, at 17:29, 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.
>
> ----------
>
> 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 via http://www.ruby-....
>

You should use the following now:

require 'rubygems'
require 'activerecord'

That should solve your problem...

Cheers,

Enrique

Chris Carter

5/23/2007 4:08:00 PM

0

On 5/23/07, Dennis Crissman <dcrissman@perimeterusa.com> 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.
>
> ----------
>
> 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 via http://www.ruby-....
>
>

try require 'active_record'

--
Chris Carter
concentrationstudios.com
brynmawrcs.com

Dennis Crissman

5/23/2007 4:32:00 PM

0

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 via http://www.ruby-....

Enrique Comba Riepenhausen

5/23/2007 4:46:00 PM

0

> 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 !
> --
> François Beausoleil
> http://blog.te...
> http://piston.ruby...

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!

Logan Capaldo

5/23/2007 4:48:00 PM

0

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).

Brady Behrman

5/23/2007 4:53:00 PM

0

unsubscribe

Brady Behrman
(757) 271-8293
brady@vbwebsites.net
http://www.VBWe...


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

On 5/23/07, Dennis Crissman <dcrissman@perimeterusa.com> 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.
>
> ----------
>
> 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 via http://www.ruby-....
>
>

try require 'active_record'

--
Chris Carter
concentrationstudios.com
brynmawrcs.com




Rick DeNatale

5/23/2007 5:18:00 PM

0

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.denh...

IPMS/USA Region 12 Coordinator
http://ipmsr12.denh...

Visit the Project Mercury Wiki Site
http://www.mercuryspace...

Jim Weirich

5/23/2007 9:59:00 PM

0

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 via http://www.ruby-....

Dennis Crissman

5/24/2007 12:39:00 PM

0

Thats great guys, thank you for the explanation.

--
Posted via http://www.ruby-....

Lloyd Linklater

5/24/2007 1:10:00 PM

0

Brady Behrman wrote:
> unsubscribe
>
> Brady Behrman
> (757) 271-8293
> brady@vbwebsites.net
> http://www.VBWe...

Does this mean that the VB guy does not like Ruby anymore? :)

--
Posted via http://www.ruby-....