From: Jim Weirich Date: 2007-05-24T06:59:14+09:00 Subject: Re: require_gem vs. gem 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-forum.com/.