這是一個延續這種原始的SO問題:Using "::" instead of "module ..." for Ruby namespacing紅寶石 - 詞法範圍VS繼承
在原來的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
有人可以提供一些解釋,爲什麼第一個電話返回555,爲什麼第二個電話返回123?
威爾森,你覺得是值得的賞金其回答以下?謝謝 – rainkinz 2013-03-07 21:37:40
提示:在你的代碼中添加兩個「puts Module.nesting」。 另請參閱:http://coderrr.wordpress.com/2008/03/11/constant-name-resolution-in-ruby/ – 2013-03-08 15:22:14