![]()
|
"http://www.w3.org/TR/REC-html40/strict.dtd">
Modules in ruby are similar to classes, except:
Actually... the Module class of module is the superclass of the Class class of class. Got that? No? Let's move on. There are two typical uses of modules. One is to collect
related methods and constants in a central location. The
The
Another use of modules is called mixin. Some OO programming langages, including C++, allow multiple inheritance, that is, inheritance from more than one superclass. A real-world example of multiple inheritance is an alarm clock; you can think of alarm clocks as belonging to the class of clocks and also the class of things with buzzers. Ruby purposely does not implement true multiple inheritance, but
the mixin technique is a good alternative. Remember that
modules cannot be instantiated or subclassed; but if we
Mixin can be thought of as a way of asking for whatever particular
properties we want to have. For example, if a class has a
working This use of modules gives us the basic functionality of multiple inheritance but allows us to represent class relationships with a simple tree structure, and so simplifies the language implementation considerably (a similar choice was made by the designers of Java).
|