Object Oriented Programming
Modules in Ruby
A module contains methods and fields for use when declaring a class.
module moduleName def moduleMethod end attr :moduleField end class classWithModule #brings all methods and fields from the module into the class object include moduleName #brings methods and fields to the class itself (i.e. static) extend moduleName end
Instance variables from the module are carried through to the class.
module Vehicle STOPSPEED = 0 attr :speed def speedUp @speed = @speed + 10 end end class Car include Vehicle def initialize @speed = 0 speedUp #from the module end end myCar = Car.new #module member is called on object >> myCar.speed => 10 class Plane extends Vehicle end #module member is called on class itself >> Plane.STOPSPEED >= 0