Including Modules
Modules, in their simplest definition, are a way to wrap code into a bundle so the code can be reused within other code without needing to be duplicated.
This definition sounds very similar to that of a Ruby class. Specifically, what makes a Ruby module distinct is that it cannot be instantiated into an object like a class. It can, however, be included in a class so the methods and variables defined in the module are accessible by the class. This refers to the mixin property of modules. Also, the methods and variables in a class can also be accessible to the code in the module. Essentially, when a module is included in a class, Ruby treats that code as if it were written right into that class. All the previous concepts we learned about classes and inheritance still apply to the module code when called.
For instance, if you call a module method from inside a class, that module method calls super
and it will call the super
class method of the class that the module...