→ Including module inside module in Ruby → → →

Basically, the question was (yes, sometimes I have doubts): if you include module A inside module B, and you include module B in class, do you have access to methods defined in module A? Or maybe you have to include module A in the class? The simple answer is that you can include module inside module and both modules will be usable from the class at that point. See below:

module Animal
  module Mammal
    def breathe
      puts 'zzzz....'
    end
  end
end

module Animal
  module Dog
    include Animal::Mammal
    def bark
      puts 'woof! woof!'
    end
  end
end

class Dingo
  include Animal::Dog
end

Dingo.new.bark
Dingo.new.breathe

And the output is here:

woof! woof!
zzzz....

Created on 14 May 2009