title: Why does the order of including modules make a difference in Ruby?
body: <p>This question is best summed up with a code example:</p>
  
  <pre><code>module TestOne
    module Foo
      def foo
        42
      end
    end
  
    module Bar
      include Foo
    end
  
    class Quux
      include Bar
    end
  end
  
  TestOne::Bar.ancestors # =&gt; [TestOne::Bar, TestOne::Foo]
  TestOne::Quux.ancestors # =&gt; [TestOne::Quux, TestOne::Bar, TestOne::Foo, Object, Kernel]
  TestOne::Quux.new.foo # =&gt; 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 # =&gt; [TestTwo::Bar, TestTwo::Foo]
  TestTwo::Quux.ancestors # =&gt; [TestTwo::Quux, TestTwo::Bar, Object, Kernel]
  TestTwo::Quux.new.foo # =&gt; 
  # ~&gt; -:40: undefined method `foo' for #&lt;TestTwo::Quux:0x24054&gt; (NoMethodError)
  </code></pre>
  
  <p>I thought that when you include a module (e.g. <code>Bar</code> inside a class <code>Foo</code>) all that Ruby stores is the fact that <code>Foo</code> includes <code>Bar</code>. So, when you call a method on Foo it looks in <code>Bar</code> for the method.</p>
  
  <p>If that was true, by the time <code>TestTwo::Quux.new.foo</code> is called I've mixed the <code>foo</code> method into <code>TestTwo::Bar</code> so it should work, right?</p>