1. Differences with classes:
A. No instance is available.
B. No subclass exists.
C. defined by module... end.
2. There are two main roles:
A. As a collection of methods or constants, such as math.
IRB (main): 001: 0> math. SQRT (4) => 2.0 IRB (main): 002: 0> Math: pi => 3.14159265358979 IRB (main): 003: 0> |
It should be noted that: operators are constants used to reference modules or classes.
B. Used for Mix-in.
We know that OO contains single inheritance, such as Java, multiple inheritance, such as C ++ or Python, and Ruby is also single inheritance. However, Min-in can also be used to implement multi-inheritance, such as interfaces in Java. The so-called mix-in is to introduce constants or methods in a module into another module or class through include.
Enumerable is an example of Mixin in the standard library. By mixing it into your class, if your each method can return an object, you will also have sort, find and other functions.
Differences between Mixin and multi-inheritance:
- The module does not generate instances.
- The module keeps the class hierarchy tree instead of the mesh.