#---Module Mixin---
Ruby doesn’t support multiple inheritance.
A ruby class can only inherit from one parent class.
But it can get the benefit of inheriting from multiple
‘entities’ through this concept of “mixing-in
one or more modules”.
Modules also act as namespace for the methods and
constants defined in them, allowing you to sandbox
them safely and also put them in separate files and
require them as needed.
module LifeSupport
def water?
true
end
def oxygen?
true
end
end
module RingSupport
def rings?
true
end
end
This module isn’t included in the
NewWorld
class below.
module StormSupport
def minimum_wind_speed_in_miles_per_hour
1200
end
end
Modules are embedded into a class using
the include
keyword.
All the methods in the included modules are available
to the object created using this class as
instance methods.
class NewWorld
include LifeSupport
include RingSupport
end
w = NewWorld.new
p w.water? # => "true"
p w.rings? # => "true"
p w.respond_to?(:minimum_wind_speed_in_miles_per_hour)
## => "false"
p w.minimum_wind_speed_in_miles_per_hour
## fails with the NoMethodError exception