Name Conflicts
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 end
Now a class, Person, includes both these modules:
class Person include Happy include Sad attr_accessor :mood def initialize ...
Get The Book of Ruby now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.