In Files

  • rubygems.rb
  • rubygems/custom_require.rb

Methods

Class/Module Index [+]

Quicksearch

Kernel

Private Instance Methods

gem(gem_name, *version_requirements) click to toggle source

Use #gem to activate a specific version of gem_name.

version_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 #gem, #require will do the right thing for you.

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

#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 rubygems.rb, line 1061
def gem(gem_name, *version_requirements) # :doc:
  skip_list = (ENV['GEM_SKIP'] || "").split(/:/)
  raise Gem::LoadError, "skipping #{gem_name}" if skip_list.include? gem_name
  Gem.activate(gem_name, *version_requirements)
end
            
require(path) click to toggle source

When RubyGems is required, #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 rubygems/custom_require.rb, line 28
def require(path) # :doc:
  gem_original_require path
rescue LoadError => load_error
  if load_error.message.end_with?(path)
    if Gem.try_activate(path)
      return gem_original_require(path)
    end
  end

  raise load_error
end
            
Also aliased as: gem_original_require

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus