Because I come from a C/C++ background, I always teach about name collisions using the canonical multiple inheritance example: Cowboy.draw and Artist.draw.
The Ruby folks have a much more entertaining example:
However, there are times when you want to group things together that don't naturally form a class.
An initial approach might be to put all these things into a file and simply load that file into any program that needs it. This is the way the C language works. However, there's a problem. Say you write a set of trigonometry functions sin, cos, and so on. You stuff them all into a file, trig.rb, for future generations to enjoy. Meanwhile, Sally is working on a simulation of good and evil, and codes up a set of her own useful routines, including beGood and sin, and sticks them into action.rb. Joe, who wants to write a program to find out how many angels can dance on the head of a pin, needs to load both trig.rb and action.rb into his program. But both define a method called sin. Bad news.
Kudos to David and Andrew for writing a great Ruby book, and for putting it online. All programming books should be available online.
1:22:07 AM
|
|