いろいろ気になったので調査.以下のようなコードを用意.
module Parent
CONST = 2
end
module Parent
module Base
BASE_CONST = 3
end
end
module Parent
module Base
class Child
class << self
def bar
puts BASE_CONST
puts CONST
end
end
end
end
end
class Hoge
include Parent::Base
def fuga
puts BASE_CONST
puts CONST
end
end
class Huga
include Parent
def baz
puts CONST
puts BASE_CONST
end
end
% irb
>> foo = Hoge.new
=> #<Hoge:0x......>
>> foo.fuga # CONST は Parent にあるので include Parent::Base だと見えない
3
NameError: uninitialized constant Hoge::CONST
from /home/ogawa/work/module-test2.rb:29:in 'fuga'
from (irb):6
>> zoo = Huga.new
=> #<Huga:0x......>
>> zoo.baz # BASE_CONST は Parent::Base にあるので include Parent だと見えない(これはあたりまえ)
2
NameError: uninitialized constant Huga::BASE_CONST
from /home/ogawa/work/module-test2.rb:38:in 'baz'
from (irb):8
>> Parent::Base::Child.bar # Child は Parent::Base にあるので,Parent, Parent::Base ともに見える
3
2
=> nil
comments powered by Disqus