西松と申します.
ネストしたクラスから親クラスでincludeしたModule内の定数を参照
する場合, 下の例のように モジュール名:: をつけなければNameError
になるようです. 直感的には モジュール名:: を省略できてもよいと
思うのですがどうでしょう. それともなにか理由があるのでしょうか.
# incmod.rb
module M
ConstM = "M"
end
class Parent
ConstP = "P"
include M
p ConstP # => "P"
p M::ConstM # => "M"
p ConstM # => "M"
class Nested
p ConstP # => "P"
p M::ConstM # => "M"
p ConstM # => uninitialized constant Parent::Nested::ConstM (NameError)
end
end
p Parent::ConstP # => "P"
p Parent::M::ConstM # => "M"
p Parent::ConstM # => "M"
p M::ConstM # => "M"
ぼくはRubyのincludeを「梱包を解いてそこに置く」というかんじで
理解しています.
--
love && peace && free_software
西松 毅