Module: Kernel
- Defined in:
- lib/minigems.rb
Instance Method Summary collapse
- #gem(name, *versions) ⇒ Object
- #gem_original_require ⇒ Object
-
#require(path) ⇒ Object
We replace Ruby’s require with our own, which is capable of loading gems on demand.
Instance Method Details
#gem(name, *versions) ⇒ Object
36 37 38 |
# File 'lib/minigems.rb', line 36 def gem(name, *versions) Gem.activate(name, *versions) end |
#gem_original_require ⇒ Object
42 |
# File 'lib/minigems.rb', line 42 alias :gem_original_require :require |
#require(path) ⇒ Object
We replace Ruby’s require 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.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/minigems.rb', line 57 def require(path) # :nodoc: gem_original_require path rescue LoadError => load_error if File.basename(path).match(Gem::MiniGems::INLINE_REGEXP) && Object.const_defined?(:Inline) # RubyInline dynamically created .so/.bundle return gem_original_require(File.join(Inline.directory, path)) elsif path == 'Win32API' && !Gem.win_platform? raise load_error elsif load_error. =~ /#{Regexp.escape path}\z/ if !path.include?('/') && Gem.activate(path) return gem_original_require(path) elsif $MINIGEMS_SKIPPABLE.include?(path) raise load_error elsif spec = Gem.searcher.find(path) Gem.activate(spec.name, "= #{spec.version}") return gem_original_require(path) end end raise load_error end |