この質問は、コード例で最もよく要約されます。
module TestOne
module Foo
def foo
42
end
end
module Bar
include Foo
end
class Quux
include Bar
end
end
TestOne::Bar.ancestors # => [TestOne::Bar, TestOne::Foo]
TestOne::Quux.ancestors # => [TestOne::Quux, TestOne::Bar, TestOne::Foo, Object, Kernel]
TestOne::Quux.new.foo # => 42
module TestTwo
class Quux
end
module Bar
end
module Foo
def foo
42
end
end
end
TestTwo::Quux.send :include, TestTwo::Bar
TestTwo::Bar.send :include, TestTwo::Foo
TestTwo::Bar.ancestors # => [TestTwo::Bar, TestTwo::Foo]
TestTwo::Quux.ancestors # => [TestTwo::Quux, TestTwo::Bar, Object, Kernel]
TestTwo::Quux.new.foo # =>
# ~> -:40: undefined method `foo' for #<TestTwo::Quux:0x24054> (NoMethodError)
モジュールを(たとえばBar
クラス内に)含めると、Rubyが格納するのは。を含むFoo
という事実だけだと思いました。したがって、Fooでメソッドを呼び出すと、そのメソッドが検索されます。Foo
Bar
Bar
それが本当なら、その時TestTwo::Quux.new.foo
までに私はfoo
メソッドを混ぜ合わせたTestTwo::Bar
ので、うまくいくはずですよね?