Что происходит, когда вы вызываете put () в конце определения require ()? - PullRequest
0 голосов
/ 14 марта 2020

Я пытаюсь перегрузить метод Kernel.require(), чтобы получить данные, необходимые для построения дерева зависимостей кода. Вот как я просто представляю новый метод require:

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

К сожалению, я обнаружил, что это тормозит вызов require() где-то еще в коде:

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

1 Ответ

2 голосов
/ 14 марта 2020

Исходная реализация Kernel#require возвращает true или false. Ваш новый метод require больше не возвращает это значение, вместо этого он всегда возвращает nil (ответ от метода p).

Я могу себе представить, что в некоторых случаях имеет смысл иметь условие в вашем коде и определите константы в зависимости от ответа require.

Вероятно, вы можете решить проблему, поменяв строки в вашем методе:

def require(name)
  puts "requiring '#{name}' in '#{caller_locations(1).first.path}'"
  super
end
...