The Ruby Programming Language

Modules

module MyLibrary
 #In here I can define my classes, etc.
end

#Modules can also be used as mixins. 
#Define a mixin
module Debug
  def who_am_i?
    "#{self.class.name} (\##{self.object_id}): #{self.to_s}"
  end
end

#If Debug was in another file would would have need to
require Debug

#Use the mixin
class Phonograph
  include Debug #adds the mixin. This just adds a dynamic link

  #....
end

ph = Phonograph.new("West End Blues")
ph.who_am_i? # Phonograph (#942232): West End Blues

José M. Vidal .

10 of 11