July 2011
Intermediate to advanced
400 pages
8h 46m
English
Module methods (those methods specifically preceded by the module name) can help protect your code from accidental name conflicts. However, no such protection is given by instance methods within modules. Let’s suppose you have two modules—one called Happy and the other called Sad. They each contain a module method called mood and an instance method called expression.
happy_sad.rb
module Happy
def Happy.mood # module method
return "happy"
end
def expression # instance method
return "smiling"
end
end
module Sad
def Sad.mood # module method
return "sad"
end
def expression # instance method
return "frowning"
end
endNow a class, Person, includes both these modules:
class Person include Happy include Sad attr_accessor :mood def initialize ...
Read now
Unlock full access