This post originated from an RSS feed registered with Ruby Buzz
by Daniel Berger.
Original Post: Mixin Surprise
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
Ah, the things you learn from reading the source and writing your own tests. I've always thought that when you mixin multiple modules with the same method it was a case of "last one wins". It turns out that isn't quite true. To wit:
module ModOne
def hello
"hello1"
end
end
module ModTwo
def hello
"hello2"
end
end
class Foo
include ModOne, ModTwo
end
class Bar
include ModOne
include ModTwo
end
puts Foo.new.hello # => hello1
puts Bar.new.hello # => hello2
So, it seems that if you call include with multiple modules the first one wins, but if you call include multiple times then the last one wins.
Am I the last knucklehead in the Ruby community to figure this out? I checked my pickaxe and that behavior isn't documented so far as I've found. It doesn't strike me as particularly consistent.
It's days like this that make me wonder if I should quit programming and just install solar panels for a living.