覆盖'require'会导致“没有这样的文件加载

时间:2014-08-08 02:35:03

标签: ruby

我删除了我原来的帖子,因为我现在可以通过一个非常短的程序简化我的问题:

 ## a.rb
 require 'tmpdir'

执行a.rb没问题,没有错误。

但如果我将require覆盖为:

## b.rb
module Kernel
  alias_method :original_require, :require

  def require name
    i = 1
    begin
      original_require name
    rescue LoadError => e
      puts "Failure #{i}: #{e}"
      i = i + 1
      retry if i < 3
    end
  end
end

require 'tmpdir'

执行b.rb将收到以下错误:

Failure 1: no such file to load -- Win32API
Failure 2: no such file to load -- Win32API
/Users/chaol/.rvm/rubies/ruby-1.9.1-p376/lib/ruby/1.9.1/tmpdir.rb:19:in `<class:Dir>': uninitialized constant Dir::Win32API (NameError)
    from /Users/chaol/.rvm/rubies/ruby-1.9.1-p376/lib/ruby/1.9.1/tmpdir.rb:9:in `<top (required)>'
    from /Users/chaol/Documents/ruby-workspace/Test/root/aaa.rb:9:in `require'
    from /Users/chaol/Documents/ruby-workspace/Test/root/aaa.rb:9:in `require'
    from /Users/chaol/Documents/ruby-workspace/Test/root/aaa.rb:20:in `<main>'

0 个答案:

没有答案