当您在Kernel.require()定义内调用Kernel.puts()时会发生什么?

问题描述 投票:0回答:1

我正在尝试构建程序代码依赖关系树,因此,从覆盖Kernel.require()语句开始,向我输出为此所需的数据。仅使用Kernel.p()方法进行输出时,一切都很好,因此此代码将为我提供所需的数据:

def require arg
  super arg
  p "including '#{arg}' in '#{caller_locations(1).first.path}'"
end

但是我注意到,当代替使用Kernel.puts()时,它对于某些'require'语句似乎工作得很好,但是对于其他我却得到了CodeRay::Encoders::PluginHost未初始化的常量错误:

Traceback (most recent call last):
    28: from ./thief:9:in `<main>'
    27: from ./thief:9:in `require_relative'
    26: from /home/siery/devel/eco-sim/lib/thief.rb:12:in `<top (required)>'
    25: from /home/siery/devel/eco-sim/lib/thief.rb:13:in `<module:Thief>'
    24: from /home/siery/devel/eco-sim/lib/thief.rb:5:in `require'
    23: from /home/siery/devel/eco-sim/lib/thief.rb:5:in `require'
    22: from /home/siery/devel/eco-sim/lib/engine.rb:2:in `<top (required)>'
    21: from /home/siery/devel/eco-sim/lib/thief.rb:5:in `require'
    20: from /home/siery/devel/eco-sim/lib/thief.rb:5:in `require'
    19: from /home/siery/devel/eco-sim/lib/screen_area.rb:1:in `<top (required)>'
    18: from /home/siery/devel/eco-sim/lib/thief.rb:5:in `require'
    17: from /home/siery/devel/eco-sim/lib/thief.rb:5:in `require'
    16: from /home/siery/devel/eco-sim/lib/map.rb:2:in `<top (required)>'
    15: from /home/siery/devel/eco-sim/lib/thief.rb:5:in `require'
    14: from /home/siery/devel/eco-sim/lib/thief.rb:5:in `require'
    13: from /home/siery/devel/eco-sim/lib/debug.rb:1:in `<top (required)>'
    12: from /home/siery/devel/eco-sim/lib/thief.rb:5:in `require'
    11: from /home/siery/devel/eco-sim/lib/thief.rb:5:in `require'
    10: from /home/siery/.rvm/gems/ruby-2.6.2@stable_project/gems/pry-0.11.3/lib/pry.rb:152:in `<top (required)>'
     9: from /home/siery/devel/eco-sim/lib/thief.rb:5:in `require'
     8: from /home/siery/devel/eco-sim/lib/thief.rb:5:in `require'
     7: from /home/siery/.rvm/gems/ruby-2.6.2@stable_project/gems/pry-0.11.3/lib/pry/color_printer.rb:2:in `<top (required)>'
     6: from /home/siery/.rvm/gems/ruby-2.6.2@stable_project/gems/pry-0.11.3/lib/pry/color_printer.rb:3:in `<class:Pry>'
     5: from /home/siery/.rvm/gems/ruby-2.6.2@stable_project/gems/pry-0.11.3/lib/pry/color_printer.rb:5:in `<class:ColorPrinter>'
     4: from /home/siery/devel/eco-sim/lib/thief.rb:5:in `require'
     3: from /home/siery/devel/eco-sim/lib/thief.rb:5:in `require'
     2: from /home/siery/.rvm/gems/ruby-2.6.2@stable_project/gems/coderay-1.1.2/lib/coderay/encoders.rb:1:in `<top (required)>'
     1: from /home/siery/.rvm/gems/ruby-2.6.2@stable_project/gems/coderay-1.1.2/lib/coderay/encoders.rb:10:in `<module:CodeRay>'
/home/siery/.rvm/gems/ruby-2.6.2@stable_project/gems/coderay-1.1.2/lib/coderay/encoders.rb:12:in `<module:Encoders>': uninitialized constant CodeRay::Encoders::PluginHost (NameError)
Did you mean?  CodeRay::PluginHos

是因为Kernel::require定义在某些Kernel::puts要求之前?

ruby kernel require coderay
1个回答
2
投票

Kernel#require的原始实现返回Kernel#requiretrue。您的新false方法不再返回该值,而是始终返回requirenil方法的响应)。

我可以想象,在某些情况下,在代码中具有条件并根据p的响应定义常量是有意义的。

您可能可以通过交换方法中的行来解决此问题:

require
© www.soinside.com 2019 - 2024. All rights reserved.