対字句スコープは、これは、このオリジナルのSO質問続きです:元SOの質問でUsing "::" instead of "module ..." for Ruby namespacingルビー - 継承
、ここで私はまだトラブルの理解を抱え提示シナリオです:
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を返す理由と、2番目のコールが123を返す理由を説明できる人がいますか?
ウィルソン555を、割り当てられていましたか? Thanks – rainkinz
ヒント:コードに2つのputsの後に "puts Module.nesting"を追加します。 これも参照してください:http://coderrr.wordpress.com/2008/03/11/constant-name-resolution-in-ruby/ –