What does ruby include when it encounters the "include module" statement?

Posted by Steve Weet on Stack Overflow See other posts from Stack Overflow or by Steve Weet
Published on 2010-03-18T09:29:42Z Indexed on 2010/03/18 9:31 UTC
Read the original article Hit count: 335

Filed under:
|

If I have the following project structure

project/   
  lib/
    subproject/
      a.rb
      b.rb
      lib.rb

where lib.rb looks like this :-

module subproject
  def foo
    do_some_stuff
  end
end

and a.rb and b.rb both need to mixin some methods within lib.rb and are both namespaced within a module like so :-

require 'subproject/lib'

module subproject
  class A
    include Subproject

    def initialize()
      foo()
    end
  end
end

What does ruby do when it encounters the include statement? How does it know that I want to only include the mixin from lib.rb rather than the whole module which includes both class A and class B, is this based purely on the require of subproject/lib or am I getting it wrong and it is including the whole of the module, including the definitions of Class A and B within themselves?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about ruby-on-rails