Resolving Method Ambiguities

Following the method-finding trail through the path of inheritance and/or module inclusion seems like a more obscure way of understanding things than simply saying, “Cello has the method #tune because it got it from ViolinFamilyMember.” But there are cases where the question of whether an object responds to a given message is trickier than this.

Let's say you write a class that mixes in two different modules (a perfectly legitimate thing to do). What happens if those two modules, by coincidence, define methods with the same name?

Consider this case:

 module Nice def salute puts "Nice to meet you." end end module Military def salute print "To show my respect, I'm hitting my forehead " puts "with the side of my index ...

Get Sams Teach Yourself Ruby in 21 Days 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.