相关疑难解决方法(0)

Ruby - 词法范围与继承

这是原始SO问题的延续:使用"::"而不是"module ..."来表示Ruby命名空间

在最初的SO问题中,这里是我仍然无法理解的情景:

FOO = 123

module Foo
  FOO = 555
end

module Foo
  class Bar
    def baz
      puts FOO
    end
  end
end

class Foo::Bar
  def glorf
    puts FOO
  end
end

puts Foo::Bar.new.baz    # -> 555
puts Foo::Bar.new.glorf  # -> 123
Run Code Online (Sandbox Code Playgroud)

有人可以提供一些解释为什么第一个呼叫返回555以及为什么第二个呼叫返回123?

ruby inheritance lexical-scope

33
推荐指数
2
解决办法
2946
查看次数

标签 统计

inheritance ×1

lexical-scope ×1

ruby ×1