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:inrequire’
from (irb):1
irb(main):002:0> exit
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.
On Thu, May 24, 2007 at 12:29:58AM +0900, Dennis C. 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).
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.
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.
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.
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.