I want to be able to switch back and forth as the instance responds to the message. I want to do this by mixing in a module and then later moving in another module to override this behavior.
Example:
module Dog
def speak
puts "woof woof"
end
end
module Cat
def speak
puts "meow"
end
end
class Animal
end
Now I want to switch back and forth how the instance Animalresponds to the message speak:
animal = Animal.new
animal.extend(Cat)
animal.speak
animal.extend(Dog)
animal.speak
animal.extend(Cat)
animal.speak
animal.extend(Dog)
animal.speak
animal.extend(Cat)
animal.speak
I expect this code to output the following:
meow
woof woof
meow
woof woof
meow
Instead, it is output as follows:
meow
woof woof
woof woof
woof woof
woof woof
Any tips on how I can get this to work as expected?
source
share