问题描述
尝试解决。 (编辑: )
文件说:
它通过打开模块Kernel
并使用别名对别名进行别名gem_original_require require
,然后重新定义 require
首先调用原始版本,如果不起作用,请查看gem。
ruby-1.8.7-p330:002> $ :.长度
=> 9
ruby-1.8.7-p330:003>要求'rubygems'
=> true
ruby-1.8.7-p330:004> $ :.长度
=> 9
ruby-1.8.7-p330:005>需要'haml'
=> true
ruby-1.8.7-p330:006> $ :.长度
=> 10
While trying to solve Gem found in irb, not in Ruby , I tried seeing what effect require 'rubygems'
had on my own installation:
$ irb
irb(main):001:0> RUBY_VERSION
=> "1.8.7"
irb(main):002:0> $:
["/usr/local/lib/site_ruby/1.8", "/usr/local/lib/site_ruby/1.8/x86_64-linux", "/usr/local/lib/site_ruby", "/usr/lib/ruby/vendor_ruby/1.8", "/usr/lib/ruby/vendor_ruby/1.8/x86_64-linux", "/usr/lib/ruby/vendor_ruby", "/usr/lib/ruby/1.8", "/usr/lib/ruby/1.8/x86_64-linux", "."]
=> nil
irb(main):003:0> require "rubygems" # Hasn't been required already
=> true
irb(main):004:0> require "rubygems" # Will return false, because it's already been required
=> false
irb(main):005:0> $: # Same as before
=> ["/usr/local/lib/site_ruby/1.8", "/usr/local/lib/site_ruby/1.8/x86_64-linux", "/usr/local/lib/site_ruby", "/usr/lib/ruby/vendor_ruby/1.8", "/usr/lib/ruby/vendor_ruby/1.8/x86_64-linux", "/usr/lib/ruby/vendor_ruby", "/usr/lib/ruby/1.8", "/usr/lib/ruby/1.8/x86_64-linux", "."]
If requring rubygems doesn't modify $:
, then how does it help find files? Does it monkeypatch require
? If so, what variable does it use to find rubygem-installed libraries?
Here's the current version of the relevant source: https://github.com/rubygems/rubygems/blob/02ead548e38ff90923444fa7c0ff9f6a5dbd87b0/lib/rubygems/custom_require.rb. (Edit: here's an earlier version (1.5.2) that more clearly expresses what happens.)
The docs say:
It does this by opening up module Kernel
and aliasing the original require with alias gem_original_require require
, then redefining require
to first call the original version, and look at the gems if that doesn't work.
So the load path is only changed when you require a gem:
ruby-1.8.7-p330 :002 > $:.length
=> 9
ruby-1.8.7-p330 :003 > require 'rubygems'
=> true
ruby-1.8.7-p330 :004 > $:.length
=> 9
ruby-1.8.7-p330 :005 > require 'haml'
=> true
ruby-1.8.7-p330 :006 > $:.length
=> 10
这篇关于如何要求rubygems帮助找到rubygem文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!