Ruby Mixins

15 Jan 2009

Q. What happens when you include two modules that define the same method in one class?

A. The method in the module added last is called!

module Tiger
  def talk
    "prrrrr!"
  end
end
module Lion
  def talk
    "roar!"
  end
end

class Liger
  include Lion
  include Tiger
end

class Tigon
  include Tiger
  include Lion
end

tigon = Tigon.new
puts "a tigon says #{tigon.talk}"

liger = Liger.new
puts "a liger says #{liger.talk}"
$ ruby cats.rb
a tigon says roar!
a liger says prrrrr!