# Tengo problemas con el ruby error.rb

**URL:** <https://www.ruby-forum.com/t/tengo-problemas-con-el-ruby-error-rb/105668>\
**Category:** Ruby\
**Created:** [June 1, 2007, 6:48am UTC](https://www.ruby-forum.com/t/tengo-problemas-con-el-ruby-error-rb/105668 "2007-06-01T06:48:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [June 1, 2007, 6:48am UTC](https://www.ruby-forum.com/t/tengo-problemas-con-el-ruby-error-rb/105668/1 "2007-06-01T06:48:21Z")

</div>

Estoy trabajando en centos 5.0, he instalado asterisk y quiero integrar  
un calendario a mi call center… pero cuando sigo los pasos de este how  
to

[http://blog.voz-ip.com/2006/consulta-los-festivos-de-un-calendario-webdav-desde-asterisk/#more-23](http://blog.voz-ip.com/2006/consulta-los-festivos-de-un-calendario-webdav-desde-asterisk/#more-23)

corre todo bien pero a la hora de que hacen una llamada y ejecuto mi  
script agi tengo este error

Script /var/lib/asterisk/agi-bin/comprobar-festivos.rb  
/usr/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:27:in  
`gem_original_requireâ€™: no such file to load â€“error.rb (LoadError) from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `requireâ€™  
from  
/usr/lib/ruby/gems/1.8/gems/ruby-agi-2.0.0/lib/ruby-agi/rs/receive\_char.rb:24  
from /usr/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:33:in  
`gem_original_requireâ€™ from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:33:in `requireâ€™  
from  
/usr/lib/ruby/gems/1.8/gems/ruby-agi-2.0.0/lib/ruby-agi/return\_status.rb:9  
from /usr/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:33:in  
`gem_original_requireâ€™ from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:33:in `requireâ€™  
from  
/usr/lib/ruby/gems/1.8/gems/ruby-agi-2.0.0/lib/ruby-agi/command.rb:40  
â€¦ 10 levelsâ€¦  
from /usr/lib/ruby/site\_ruby/1.8/rubygems.rb:228:in `activateâ€™ from /usr/lib/ruby/site_ruby/1.8/rubygems.rb:66:in `active\_gem\_with\_optionsâ€™  
from /usr/lib/ruby/site\_ruby/1.8/rubygems.rb:59:in `require\_gemâ€™  
from /var/lib/asterisk/agi-bin/comprobar-festivos.rb:20  
â€” AGI Script comprobar-festivos.rb completed, returning 0

---

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [June 1, 2007, 3:26pm UTC](https://www.ruby-forum.com/t/tengo-problemas-con-el-ruby-error-rb/105668/2 "2007-06-01T15:26:03Z")

</div>

Creo que debe usar ‘require’ en vez de ‘require\_gem’ en linea 20. Si  
no me equivoco, con la última versión de rubygems ya no hace falta  
´require\_gem´.

–Brenton

---

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [June 2, 2007, 10:47pm UTC](https://www.ruby-forum.com/t/tengo-problemas-con-el-ruby-error-rb/105668/3 "2007-06-02T22:47:46Z")

</div>

Muchisimas gracias por el dato… corregiste varias lineas. pero aun  
sigue apareciendo este error… ☹

Launched AGI Script /var/lib/asterisk/agi-bin/comprobar-festivos.rb  
/usr/lib/ruby/site\_ruby/1.8/rubygems.rb:66:in `active_gem_with_options': undefined method`activate’ for Gem:Module (NoMethodError)  
from /usr/lib/ruby/site\_ruby/1.8/rubygems.rb:59:in `require' from /usr/lib/ruby/site_ruby/1.8/rubygems.rb:75 from /var/lib/asterisk/agi-bin/comprobar-festivos.rb:17:in`require’  
from /var/lib/asterisk/agi-bin/comprobar-festivos.rb:17  
– AGI Script comprobar-festivos.rb completed, returning 0

muchas gracias de ante mano por tu ayuda y por tu tiempo.

---

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [June 3, 2007, 1:15am UTC](https://www.ruby-forum.com/t/tengo-problemas-con-el-ruby-error-rb/105668/4 "2007-06-03T01:15:55Z")

</div>

¿Sabe Ud. la versión de rubygems que tiene? Me pregunto si su  
versiónno está al día.

En su código no ha declarado otro modulo Gem, verdad¿? Me parece un  
tanto extraño el NoMethodError. Pase un tiempo mirando el código de  
rubygems y no encuentro el sitio donde se declara Gem#activate.

Does anyone familiar with rubygems know how the Gem module is suppose  
to respond to activate?

---

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [June 3, 2007, 3:05am UTC](https://www.ruby-forum.com/t/tengo-problemas-con-el-ruby-error-rb/105668/5 "2007-06-03T03:05:55Z")

</div>

Estoy usando Centos 5.0 kernel-2.6.18-8.el5 ruby-1.8.5-5.el5  
ruby-rdoc-1.8.5-5.el5 rubygems-0.9.0  
No se porque no encuentra ese modulo no he instalado ninguno ni lo he  
eliminado.  
Este es el archivo

# rubygems.rb

#!/usr/bin/env ruby

# -_- ruby -_-

#–

# Copyright 2006 by Chad F., Rich Kilmer, Jim W. and others.

# All rights reserved.

# See LICENSE.txt for permissions.

#++

require ‘rbconfig’

module Gem  
class LoadError \< ::LoadError  
attr\_accessor :name, :version\_requirement  
end  
end

module Kernel

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

# 

def gem(gem\_name, \*version\_requirements)  
active\_gem\_with\_options(gem\_name, version\_requirements)  
end

# Same as the +gem+ command, but will also require a file if the gem

# provides an auto-required file name.

# 

# DEPRECATED! Use +gem+ instead.

# 

def require\_gem(gem\_name, \*version\_requirements)  
active\_gem\_with\_options(gem\_name,  
version\_requirements, :auto\_require=\>true)  
end

def active\_gem\_with\_options(gem\_name, version\_requirements,  
options={})  
skip\_list = (ENV[‘GEM\_SKIP’] || “”).split(/:/)  
raise Gem::LoadError, “skipping #{gem\_name}” if skip\_list.include?  
gem\_name  
Gem.activate(gem\_name, options[:auto\_require],  
\*version\_requirements)  
end  
private :active\_gem\_with\_options  
end

# Main module to hold all RubyGem classes/modules.

# 

module Gem  
require ‘rubygems/rubygems\_version.rb’

class Exception \< RuntimeError  
end

class OperationNotSupportedError \< Gem::Exception  
end

RubyGemsPackageVersion = RubyGemsVersion

DIRECTORIES = [‘cache’, ‘doc’, ‘gems’, ‘specifications’]

@@source\_index = nil

class \<\< self

```
def manage_gems
  require 'rubygems/user_interaction'
  require 'rubygems/builder'
  require 'rubygems/format'
  require 'rubygems/remote_installer'
  require 'rubygems/installer'
  require 'rubygems/validator'
  require 'rubygems/doc_manager'
  require 'rubygems/cmd_manager'
  require 'rubygems/gem_runner'
  require 'rubygems/config_file'
end

# Returns an Cache of specifications that are in the Gem.path
#
# return:: [Gem::SourceIndex] Index of installed Gem::Specifications
#
def source_index
  @@source_index ||= SourceIndex.from_installed_gems
end

# Provide an alias for the old name.
alias cache source_index

# The directory path where Gems are to be installed.
#
# return:: [String] The directory path
#
def dir
  @gem_home ||= nil
  set_home(ENV['GEM_HOME'] || default_dir) unless @gem_home
  @gem_home
end

# List of directory paths to search for Gems.
#
# return:: [List<String>] List of directory paths.
#
def path
  @gem_path ||= nil
  set_paths(ENV['GEM_PATH']) unless @gem_path
  @gem_path
end

# The home directory for the user.
def user_home
  @user_home ||= find_home
end

# Return the path to standard location of the users .gemrc file.
def config_file
  File.join(Gem.user_home, '.gemrc')
end

# The standard configuration object for gems.
def configuration
  @configuration ||= {}
end

# Use the given configuration object (which implements the
# ConfigFile protocol) as the standard configuration object.
def configuration=(config)
  @configuration = config
end

# Return the path the the data directory specified by the gem
# name. If the package is not available as a gem, return nil.
def datadir(gem_name)
  return nil if @loaded_specs.nil?
  spec = @loaded_specs[gem_name]
  return nil if spec.nil?
  File.join(spec.full_gem_path, 'data', gem_name)
end

# Return the Ruby command to use to execute the Ruby interpreter.
def ruby
  "ruby"
end

# Activate a gem (i.e. add it to the Ruby load path). The gem
# must satisfy all the specified version constraints. If
# +autorequire+ is true, then automatically require the specified
# autorequire file in the gem spec.
#
# Returns true if the gem is loaded by this call, false if it is
# already loaded, or an exception otherwise.
#
def activate(gem, autorequire, *version_requirements)
  @loaded_specs ||= Hash.new
  unless version_requirements.size > 0
    version_requirements = [">= 0.0.0"]
  end
  unless gem.respond_to?(:name) && 

```

gem.respond\_to?(:version\_requirements)  
gem = Gem::Dependency.new(gem, version\_requirements)  
end

```
  matches = Gem.source_index.find_name(gem.name, 

```

gem.version\_requirements)  
report\_activate\_error(gem) if matches.empty?

```
  if @loaded_specs[gem.name]

```

# This gem is already loaded. If the currently loaded gem is

# not in the list of candidate gems, then we have a version

# conflict.

existing\_spec = @loaded\_specs[gem.name]  
if ! matches.any? { |spec| spec.version == existing\_spec.version }  
fail Gem::Exception, “can’t activate #{gem}, already activated  
#{existing\_spec.full\_name}]”  
end  
return false  
end

```
  # new load
  spec = matches.last
  if spec.loaded?

```

return false unless autorequire  
result = spec.autorequire ? require(spec.autorequire) : false  
return result || false  
end

```
  spec.loaded = true
  @loaded_specs[spec.name] = spec

  # Load dependent gems first
  spec.dependencies.each do |dep_gem|

```

activate(dep\_gem, autorequire)  
end

```
  # add bin dir to require_path
  if(spec.bindir) then

```

spec.require\_paths \<\< spec.bindir  
end

```
  # Now add the require_paths to the LOAD_PATH
  spec.require_paths.each do |path|

```

$:.unshift File.join(spec.full\_gem\_path, path)  
end

```
  if autorequire && spec.autorequire then
    Array(spec.autorequire).each do |a_lib|
      require a_lib
    end
  end

  return true
end

# Report a load error during activation. The message of load
# error depends on whether it was a version mismatch or if there
# are not gems of any version by the requested name.
def report_activate_error(gem)
  matches = Gem.source_index.find_name(gem.name)
  if matches.size==0

```

error = Gem::LoadError.new(  
“Could not find RubyGem #{gem.name}  
(#{gem.version\_requirements})\n”)  
else  
error = Gem::LoadError.new(  
"RubyGem version error: " +  
“#{gem.name}(#{matches.first.version} not  
#{gem.version\_requirements})\n”)  
end  
error.name = gem.name  
error.version\_requirement = gem.version\_requirements  
raise error  
end  
private :report\_activate\_error

```
# Reset the +dir+ and +path+ values. The next time +dir+ or +path+
# is requested, the values will be calculated from scratch. This is
# mainly used by the unit tests to provide test isolation.
#
def clear_paths
  @gem_home = nil
  @gem_path = nil
  @@source_index = nil
end

# Use the +home+ and (optional) +paths+ values for +dir+ and +path+.
# Used mainly by the unit tests to provide environment isolation.
#
def use_paths(home, paths=[])
  clear_paths
  set_home(home) if home
  set_paths(paths.join(File::PATH_SEPARATOR)) if paths
end

# Return a list of all possible load paths for all versions for
# all gems in the Gem installation.
#
def all_load_paths
  result = []
  Gem.path.each do |gemdir|
    each_load_path(all_partials(gemdir)) do |load_path|
      result << load_path
    end
  end
  result
end

# Return a list of all possible load paths for the latest version
# for all gems in the Gem installation.
def latest_load_paths
  result = []
  Gem.path.each do |gemdir|
    each_load_path(latest_partials(gemdir)) do |load_path|
      result << load_path
    end
  end
  result
end

def required_location(gemname, libfile, *version_constraints)
  version_constraints = [">0"] if version_constraints.empty?
  matches = Gem.source_index.find_name(gemname, version_constraints)
  return nil if matches.empty?
  spec = matches.last
  spec.require_paths.each do |path|
    result = File.join(spec.full_gem_path, path, libfile)
    return result if File.exists?(result)
  end
  nil
end

private

# Return all the partial paths in the given +gemdir+.
def all_partials(gemdir)
  Dir[File.join(gemdir, 'gems/*')]
end

# Return only the latest partial paths in the given +gemdir+.
def latest_partials(gemdir)
  latest = {}
  all_partials(gemdir).each do |gp|
    base = File.basename(gp)
    matches = /(.*)-((\d+\.)*\d+)/.match(base)
    name, version = [matches[1], matches[2]]
    ver = Gem::Version.new(version)
    if latest[name].nil? || ver > latest[name][0]
      latest[name] = [ver, gp]
    end
  end
  latest.collect { |k,v| v[1] }
end

# Expand each partial gem path with each of the required paths
# specified in the Gem spec. Each expanded path is yielded.
def each_load_path(partials)
  partials.each do |gp|
    base = File.basename(gp)
    specfn = File.join(dir, "specifications", base + ".gemspec")
    if File.exist?(specfn)
      spec = eval(File.read(specfn))
      spec.require_paths.each do |rp|
        yield(File.join(gp, rp))
      end
    else
      filename = File.join(gp, 'lib')
      yield(filename) if File.exist?(filename)
    end
  end
end

# Set the Gem home directory (as reported by +dir+).
def set_home(home)
  @gem_home = home
  ensure_gem_subdirectories(@gem_home)
end

# Set the Gem search path (as reported by +path+).
def set_paths(gpaths)
  if gpaths
    @gem_path = gpaths.split(File::PATH_SEPARATOR)
    @gem_path << Gem.dir
  else
    @gem_path = [Gem.dir]
  end
  @gem_path.uniq!
  @gem_path.each do |gp| ensure_gem_subdirectories(gp) end
end

# Some comments from the ruby-talk list regarding finding the home
# directory:
#
# I have HOME, USERPROFILE and HOMEDRIVE + HOMEPATH. Ruby seems
# to be depending on HOME in those code samples. I propose that
# it should fallback to USERPROFILE and HOMEDRIVE + HOMEPATH (at
# least on Win32).
#
def find_home
  ['HOME', 'USERPROFILE'].each do |homekey|
    return ENV[homekey] if ENV[homekey]
  end
  if ENV['HOMEDRIVE'] && ENV['HOMEPATH']
    return "#{ENV['HOMEDRIVE']}:#{ENV['HOMEPATH']}"
  end
  begin
    File.expand_path("~")
  rescue StandardError => ex
    if File::ALT_SEPARATOR
      "C:/"
    else
      "/"
    end
  end
end

public

# Default home directory path to be used if an alternate value is
# not specified in the environment.
def default_dir
  ## rbconfig = 

```

Dir.glob("{#{($LOAD\_PATH).join(’,’)}}/rbconfig.rb").first  
## if rbconfig  
## module\_eval File.read(rbconfig) unless  
const\_defined?(“Config”)  
## else  
## require ‘rbconfig’  
## end  
#  
# Note on above code: we have an issue if a Config class is  
# already defined and we load ‘rbconfig’. The above code is  
# supposed to work around that but it’s been commented out. In  
# any case, I moved “require ‘rbconfig’” to the top of this  
# file, because there was a circular dependency between this  
# method and our custom require. In any case, rbconfig is a  
# fundamental RubyGems dependency, so it might as well be up the  
# top. – Gavin S., 2004-12-12  
#  
if defined? RUBY\_FRAMEWORK\_VERSION  
return File.join(File.dirname(Config::CONFIG[“sitedir”]),  
“Gems”)  
else  
File.join(Config::CONFIG[‘libdir’], ‘ruby’, ‘gems’,  
Config::CONFIG[‘ruby\_version’])  
end  
end

```
private

# Quietly ensure the named Gem directory contains all the proper
# subdirectories. If we can't create a directory due to a
# permission problem, then we will silently continue.
def ensure_gem_subdirectories(gemdir)
  DIRECTORIES.each do |filename|
    fn = File.join(gemdir, filename)
    unless File.exists?(fn)
      require 'fileutils'
      FileUtils.mkdir_p(fn) rescue nil
    end
  end
end

```

end  
end

# Modify the non-gem version of datadir to handle gem package names.

require ‘rbconfig/datadir’  
module Config  
class \<\< self  
alias gem\_original\_datadir datadir

```
# Return the path to the data directory associated with the named
# package. If the package is loaded as a gem, return the gem
# specific data directory. Otherwise return a path to the share
# area as define by "#{Config::CONFIG['datadir']}/#{package_name}".
def datadir(package_name)
  Gem.datadir(package_name) || 

```

Config.gem\_original\_datadir(package\_name)  
end  
end  
end

# require ‘rubygems/source\_index’ require ‘rubygems/specification’ require ‘rubygems/security’ require ‘rubygems/version’ #require ‘rubygems/loadpath\_manager’ # custom\_require replaces this require ‘rubygems/custom\_require’

---

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [June 3, 2007, 3:43am UTC](https://www.ruby-forum.com/t/tengo-problemas-con-el-ruby-error-rb/105668/6 "2007-06-03T03:43:57Z")

</div>

Acabo de actualizar el Rubygem 0.9.0 al 0.9.4 con “gem update --system”  
ahora ya tengo el rubygems 0.9.4 corregi el error del require que me  
dijiste y ahora aparece este error

# ================================================================================= Launched AGI Script /var/lib/asterisk/agi-bin/comprobar-festivos.rb /usr/lib/ruby/site\_ruby/1.8/rubygems.rb:84:Warning: require\_gem is obsolete. Use gem instead. /usr/lib/ruby/site\_ruby/1.8/rubygems.rb:76:in `active_gem_with_options': undefined method`activate’ for Gem:Module (NoMethodError) from /usr/lib/ruby/site\_ruby/1.8/rubygems.rb:61:in `require' from /usr/lib/ruby/site_ruby/1.8/rubygems.rb:84 from /var/lib/asterisk/agi-bin/comprobar-festivos.rb:17:in`require’ from /var/lib/asterisk/agi-bin/comprobar-festivos.rb:17 – AGI Script comprobar-festivos.rb completed, returning 0

# que puedo hacer y el nuevo rubygems.rb es e siguiente

#!/usr/bin/env ruby

# -_- ruby -_-

#–

# Copyright 2006 by Chad F., Rich Kilmer, Jim W. and others.

# All rights reserved.

# See LICENSE.txt for permissions.

#++

require ‘rbconfig’

module Gem  
class LoadError \< ::LoadError  
attr\_accessor :name, :version\_requirement  
end  
end

module Kernel

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

# 

def gem(gem\_name, \*version\_requirements)  
active\_gem\_with\_options(gem\_name, version\_requirements)  
end

# Same as the +gem+ command, but will also require a file if the gem

# provides an auto-required file name.

# 

# DEPRECATED! Use +gem+ instead.

# 

def require(gem\_name, \*version\_requirements)  
file, lineno = location\_of\_caller  
warn “#{file}:#{lineno}⚠ require\_gem is obsolete. Use gem  
instead.”  
active\_gem\_with\_options(gem\_name, version\_requirements,  
:auto\_require=\>true)  
end

# Return the file name (string) and line number (integer) of the

caller of

# the caller of this method.

def location\_of\_caller  
file, lineno = caller[1].split(’:’)  
lineno = lineno.to\_i  
[file, lineno]  
end  
private :location\_of\_caller

def active\_gem\_with\_options(gem\_name, version\_requirements,  
options={})  
skip\_list = (ENV[‘GEM\_SKIP’] || “”).split(/:/)  
raise Gem::LoadError, “skipping #{gem\_name}” if skip\_list.include?  
gem\_name  
Gem.activate(gem\_name, options[:auto\_require],  
\*version\_requirements)  
end  
private :active\_gem\_with\_options  
end

# Main module to hold all RubyGem classes/modules.

# 

module Gem  
require ‘rubygems/rubygems\_version.rb’  
require ‘thread’

MUTEX = Mutex.new

class Exception \< RuntimeError  
end

class OperationNotSupportedError \< Gem::Exception  
end

RubyGemsPackageVersion = RubyGemsVersion

DIRECTORIES = [‘cache’, ‘doc’, ‘gems’, ‘specifications’]

@@source\_index = nil

@configuration = nil  
@loaded\_specs = {}

class \<\< self

```
attr_reader :loaded_specs

def manage_gems
  require 'rubygems/user_interaction'
  require 'rubygems/builder'
  require 'rubygems/format'
  require 'rubygems/remote_installer'
  require 'rubygems/installer'
  require 'rubygems/validator'
  require 'rubygems/doc_manager'
  require 'rubygems/command_manager'
  require 'rubygems/gem_runner'
  require 'rubygems/config_file'
end

# Returns an Cache of specifications that are in the Gem.path
#
# return:: [Gem::SourceIndex] Index of installed Gem::Specifications
#
def source_index
  @@source_index ||= SourceIndex.from_installed_gems
end

# Provide an alias for the old name.
alias cache source_index

# The directory path where Gems are to be installed.
#
# return:: [String] The directory path
#
def dir
  @gem_home ||= nil
  set_home(ENV['GEM_HOME'] || default_dir) unless @gem_home
  @gem_home
end

# The directory path where executables are to be installed.
#
def bindir(install_dir=Gem.dir)
  return File.join(install_dir, 'bin') unless install_dir == 

```

Gem.default\_dir

```
  if defined? RUBY_FRAMEWORK_VERSION then # mac framework support
    File.join(File.dirname(Config::CONFIG["sitedir"]),
              File.basename(Config::CONFIG["bindir"]))
  else # generic install
    Config::CONFIG['bindir']
  end
end

# List of directory paths to search for Gems.
#
# return:: [List<String>] List of directory paths.
#
def path
  @gem_path ||= nil
  set_paths(ENV['GEM_PATH']) unless @gem_path
  @gem_path
end

# The home directory for the user.
def user_home
  @user_home ||= find_home
end

# Return the path to standard location of the users .gemrc file.
def config_file
  File.join(Gem.user_home, '.gemrc')
end

# The standard configuration object for gems.
def configuration
  return @configuration if @configuration

  @configuration = {}
  class << @configuration
    undef_method :verbose # HACK RakeFileUtils pollution
  end if @configuration.respond_to? :verbose

  def @configuration.method_missing(sym, *args, &block)
    if args.empty?
      self[sym]
    else
      super
    end
  end

  @configuration
end

# Use the given configuration object (which implements the
# ConfigFile protocol) as the standard configuration object.
def configuration=(config)
  @configuration = config
end

# Return the path the the data directory specified by the gem
# name. If the package is not available as a gem, return nil.
def datadir(gem_name)
  spec = @loaded_specs[gem_name]
  return nil if spec.nil?
  File.join(spec.full_gem_path, 'data', gem_name)
end

# Return the searcher object to search for matching gems.
def searcher
  MUTEX.synchronize do
    @searcher ||= Gem::GemPathSearcher.new
  end
end

# Return the Ruby command to use to execute the Ruby interpreter.
def ruby
  "ruby"
end

# Activate a gem (i.e. add it to the Ruby load path). The gem
# must satisfy all the specified version constraints. If
# +autorequire+ is true, then automatically require the specified
# autorequire file in the gem spec.
#
# Returns true if the gem is loaded by this call, false if it is
# already loaded, or an exception otherwise.
#
def activate(gem, autorequire, *version_requirements)
  unless version_requirements.size > 0
    version_requirements = [">= 0.0.0"]
  end
  unless gem.respond_to?(:name) && 

```

gem.respond\_to?(:version\_requirements)  
gem = Gem::Dependency.new(gem, version\_requirements)  
end

```
  matches = Gem.source_index.find_name(gem.name, 

```

gem.version\_requirements)  
report\_activate\_error(gem) if matches.empty?

```
  if @loaded_specs[gem.name]
    # This gem is already loaded. If the currently loaded gem is
    # not in the list of candidate gems, then we have a version
    # conflict.
    existing_spec = @loaded_specs[gem.name]
    if ! matches.any? { |spec| spec.version == existing_spec.version 

```

}  
fail Gem::Exception, “can’t activate #{gem}, already activated  
#{existing\_spec.full\_name}]”  
end  
return false  
end

```
  # new load
  spec = matches.last
  if spec.loaded?
    return false unless autorequire
    result = spec.autorequire ? require(spec.autorequire) : false
    return result || false
  end

  spec.loaded = true
  @loaded_specs[spec.name] = spec

  # Load dependent gems first
  spec.dependencies.each do |dep_gem|
    activate(dep_gem, autorequire)
  end

  # bin directory must come before library directories
  spec.require_paths.unshift spec.bindir if spec.bindir

  require_paths = spec.require_paths.map do |path|
    File.join spec.full_gem_path, path
  end

  sitelibdir = Config::CONFIG['sitelibdir']

  # gem directories must come after -I and ENV['RUBYLIB']
  $:.insert($:.index(sitelibdir), *require_paths)

  # Now autorequire
  if autorequire && spec.autorequire then # DEPRECATED
    Array(spec.autorequire).each do |a_lib|
      require a_lib
    end
  end

  return true
end

# Report a load error during activation. The message of load
# error depends on whether it was a version mismatch or if there
# are not gems of any version by the requested name.
def report_activate_error(gem)
  matches = Gem.source_index.find_name(gem.name)
  if matches.size==0
    error = Gem::LoadError.new(
      "Could not find RubyGem #{gem.name} 

```

(#{gem.version\_requirements})\n")  
else  
error = Gem::LoadError.new(  
"RubyGem version error: " +  
“#{gem.name}(#{matches.first.version} not  
#{gem.version\_requirements})\n”)  
end  
error.name = gem.name  
error.version\_requirement = gem.version\_requirements  
raise error  
end  
private :report\_activate\_error

```
# Reset the +dir+ and +path+ values. The next time +dir+ or +path+
# is requested, the values will be calculated from scratch. This is
# mainly used by the unit tests to provide test isolation.
#
def clear_paths
  @gem_home = nil
  @gem_path = nil
  @@source_index = nil
end

# Use the +home+ and (optional) +paths+ values for +dir+ and +path+.
# Used mainly by the unit tests to provide environment isolation.
#
def use_paths(home, paths=[])
  clear_paths
  set_home(home) if home
  set_paths(paths.join(File::PATH_SEPARATOR)) if paths
end

# Return a list of all possible load paths for all versions for
# all gems in the Gem installation.
#
def all_load_paths
  result = []
  Gem.path.each do |gemdir|
    each_load_path(all_partials(gemdir)) do |load_path|
      result << load_path
    end
  end
  result
end

# Return a list of all possible load paths for the latest version
# for all gems in the Gem installation.
def latest_load_paths
  result = []
  Gem.path.each do |gemdir|
    each_load_path(latest_partials(gemdir)) do |load_path|
      result << load_path
    end
  end
  result
end

def required_location(gemname, libfile, *version_constraints)
  version_constraints = [">0"] if version_constraints.empty?
  matches = Gem.source_index.find_name(gemname, version_constraints)
  return nil if matches.empty?
  spec = matches.last
  spec.require_paths.each do |path|
    result = File.join(spec.full_gem_path, path, libfile)
    return result if File.exists?(result)
  end
  nil
end

def suffixes
  ['', '.rb', '.rbw', '.so', '.bundle', '.dll', '.sl', '.jar']
end

def suffix_pattern
  @suffix_pattern ||= "{#{suffixes.join(',')}}"
end

private

# Return all the partial paths in the given +gemdir+.
def all_partials(gemdir)
  Dir[File.join(gemdir, 'gems/*')]
end

# Return only the latest partial paths in the given +gemdir+.
def latest_partials(gemdir)
  latest = {}
  all_partials(gemdir).each do |gp|
    base = File.basename(gp)
    matches = /(.*)-((\d+\.)*\d+)/.match(base)
    name, version = [matches[1], matches[2]]
    ver = Gem::Version.new(version)
    if latest[name].nil? || ver > latest[name][0]
      latest[name] = [ver, gp]
    end
  end
  latest.collect { |k,v| v[1] }
end

# Expand each partial gem path with each of the required paths
# specified in the Gem spec. Each expanded path is yielded.
def each_load_path(partials)
  partials.each do |gp|
    base = File.basename(gp)
    specfn = File.join(dir, "specifications", base + ".gemspec")
    if File.exist?(specfn)
      spec = eval(File.read(specfn))
      spec.require_paths.each do |rp|
        yield(File.join(gp, rp))
      end
    else
      filename = File.join(gp, 'lib')
      yield(filename) if File.exist?(filename)
    end
  end
end

# Set the Gem home directory (as reported by +dir+).
def set_home(home)
  @gem_home = home
  ensure_gem_subdirectories(@gem_home)
end

# Set the Gem search path (as reported by +path+).
def set_paths(gpaths)
  if gpaths
    @gem_path = gpaths.split(File::PATH_SEPARATOR)
    @gem_path << Gem.dir
  else
    @gem_path = [Gem.dir]
  end
  @gem_path.uniq!
  @gem_path.each do |gp| ensure_gem_subdirectories(gp) end
end

# Some comments from the ruby-talk list regarding finding the home
# directory:
#
# I have HOME, USERPROFILE and HOMEDRIVE + HOMEPATH. Ruby seems
# to be depending on HOME in those code samples. I propose that
# it should fallback to USERPROFILE and HOMEDRIVE + HOMEPATH (at
# least on Win32).
#
def find_home
  ['HOME', 'USERPROFILE'].each do |homekey|
    return ENV[homekey] if ENV[homekey]
  end
  if ENV['HOMEDRIVE'] && ENV['HOMEPATH']
    return "#{ENV['HOMEDRIVE']}:#{ENV['HOMEPATH']}"
  end
  begin
    File.expand_path("~")
  rescue StandardError => ex
    if File::ALT_SEPARATOR
      "C:/"
    else
      "/"
    end
  end
end

public

# Default home directory path to be used if an alternate value is
# not specified in the environment.
def default_dir
  if defined? RUBY_FRAMEWORK_VERSION
    return File.join(File.dirname(Config::CONFIG["sitedir"]), 

```

“Gems”)  
else  
File.join(Config::CONFIG[‘libdir’], ‘ruby’, ‘gems’,  
Config::CONFIG[‘ruby\_version’])  
end  
end

```
private

# Quietly ensure the named Gem directory contains all the proper
# subdirectories. If we can't create a directory due to a
# permission problem, then we will silently continue.
def ensure_gem_subdirectories(gemdir)
  DIRECTORIES.each do |filename|
    fn = File.join(gemdir, filename)
    unless File.exist?(fn)
      require 'fileutils'
      FileUtils.mkdir_p(fn) rescue nil
    end
  end
end

```

end  
end

# Modify the non-gem version of datadir to handle gem package names.

require ‘rbconfig/datadir’  
module Config # :nodoc:  
class \<\< self  
alias gem\_original\_datadir datadir

```
# Return the path to the data directory associated with the named
# package. If the package is loaded as a gem, return the gem
# specific data directory. Otherwise return a path to the share
# area as define by "#{Config::CONFIG['datadir']}/#{package_name}".
def datadir(package_name)
  Gem.datadir(package_name) || 

```

Config.gem\_original\_datadir(package\_name)  
end  
end  
end

require ‘rubygems/source\_index’  
require ‘rubygems/specification’  
require ‘rubygems/security’  
require ‘rubygems/version’  
require ‘rubygems/custom\_require’
