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
34 35 36 |
# File 'lib/minigems.rb', line 34 def gem(name, *versions) Gem.activate(name, *versions) end |
#gem_original_require ⇒ Object
40 |
# File 'lib/minigems.rb', line 40 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.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/minigems.rb', line 55 def require(path) # :nodoc: gem_original_require path rescue LoadError => load_error if File.basename(path).match(Gem::MiniGems::INLINE_REGEXP) return true # RubyInline dynamicly created .so/.bundle elsif path == 'Win32API' && !Gem.win_platform? raise load_error elsif load_error. =~ /#{Regexp.escape path}\z/ if !path.include?('/') && (match = Gem.find_name(path)) Gem.activate_gem_from_path(match.first) return gem_original_require(path) elsif spec = Gem.searcher.find(path) Gem.activate(spec.name, "= #{spec.version}") return gem_original_require(path) end end raise load_error end |