Ruby has also the `module'. Modules in ruby are very similar to classes but there exist the following three differences:
module ... end
.
Actually, the Module class of module is the superclass of the Class class of class.
Now, roughly saying, there are two uses of the module. First, to collect the methods or the constants. For example, the Math module in standard library plays such a roll.
ruby> Math.sqrt(2) 1.41421 ruby> Math::PI 3.14159
Oh, I have given no explanation for `::'; It is the operator to refer the constants of a module or a class. When we refer directly to the methods or the constants of a method, we use the `include' method.
ruby> include Math Object ruby> sqrt(2) 1.41421 ruby> PI 3.14159
Another use of the module is called `mixin'. This use is little complex so should be explained in detail.
Some Object-Oriented programming langages other than ruby have the feature to realize inheritance from plural superclasses (this feature is called multiple-inheritance). Though, ruby purposely doesn't have this feature. In the altanative way, we can make it by mixin with the module.
As said above, the module is works like the class; The methods or the constants of a module can't be inherited but be appended to other modules or classes by use of include. So, doing include the definition of a module, we can mix the property into the class. It is called mixin.
Enumerable are the instances of mixin appearing in the standard library. By mixin this module to a class whose the `each' method returns each element, the class get the features: `sort', `find', etc.
The following differaces exist between multiple-inheritance and mixin:
Each of them inhibit complexty of the relationships among the classes. It is because of ruby doesn't have multiple-inheritance. Imagine the situation that the classes has many superclasses and the relationship of instance forms of a network. Situations like this are too complex to understand for the brain of the human being, at least my brain...
On the other hand, mixin makes it simple as just `the collection of particular properties all we wanna add'.
So, even in the language with multiple-inheritance, it is considered that the smart way is virtually mixin by multiple-inheriting classes like a collection of abstract properties. We advanced this idea in ruby, by deleteing multiple-inheritance and allowing mixin only.