Class Index [+]

Quicksearch

Kernel

Public Instance Methods

gem_original_require(path) click to toggle source

The Kernel#require from before RubyGems was loaded.

Alias for: require

Private Instance Methods

gem(gem_name, *requirements) click to toggle source

Use Kernel#gem to activate a specific version of gem_name.

requirements is a list of version requirements that the specified gem must match, most commonly “= example.version.number“. See Gem::Requirement for how to specify a version requirement.

If you will be activating the latest version of a gem, there is no need to call Kernel#gem, Kernel#require will do the right thing for you.

Kernel#gem returns true if the gem was activated, otherwise false. If the gem could not be found, didn’t match the version requirements, or a different version was already activated, an exception will be raised.

Kernel#gem should be called before any require statements (otherwise RubyGems may load a conflicting library version).

In older RubyGems versions, the environment variable GEM_SKIP could be used to skip activation of specified gems, for example to test out changes that haven’t been installed yet. Now RubyGems defers to -I and the RUBYLIB environment variable to skip activation of a gem.

Example:

  GEM_SKIP=libA:libB ruby -I../libA -I../libB ./mycode.rb
      # File lib/rubygems.rb, line 1200
1200:   def gem(gem_name, *requirements) # :doc:
1201:     skip_list = (ENV['GEM_SKIP'] || "").split(/:/)
1202:     raise Gem::LoadError, "skipping #{gem_name}" if skip_list.include? gem_name
1203:     spec = Gem::Dependency.new(gem_name, *requirements).to_spec
1204:     spec.activate if spec
1205:   end
require(path) click to toggle source

When RubyGems is required, Kernel#require is replaced with our own which is capable of loading gems on demand.

When you call require 'x', this is what happens:

  • If the file can be loaded from the existing Ruby loadpath, it is.

  • Otherwise, installed gems are searched for a file that matches. If it’s found in gem ‘y’, that gem is activated (added to the loadpath).

The normal require functionality of returning false if that file has already been loaded is preserved.

    # File lib/rubygems/custom_require.rb, line 34
34:   def require path
35:     if Gem.unresolved_deps.empty? or Gem.loaded_path? path then
36:       gem_original_require path
37:     else
38:       spec = Gem::Specification.find { |s|
39:         s.activated? and s.contains_requirable_file? path
40:       }
41: 
42:       unless spec then
43:         found_specs = Gem::Specification.find_in_unresolved path
44:         unless found_specs.empty? then
45:           found_specs = [found_specs.last]
46:         else
47:           found_specs = Gem::Specification.find_in_unresolved_tree path
48:         end
49: 
50:         found_specs.each do |found_spec|
51:           found_spec.activate
52:         end
53:       end
54: 
55:       return gem_original_require path
56:     end
57:   rescue LoadError => load_error
58:     if load_error.message.end_with?(path) and Gem.try_activate(path) then
59:       return gem_original_require(path)
60:     end
61: 
62:     raise load_error
63:   end
Also aliased as: gem_original_require

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.