
Ruby Mixins – Include
Ruby, like any object-oriented language, relies on inheritance. Due to the limitation of inheriting from only one class at a time, the language creators proposed a mechanism of mixins - adding modules to classes. This allows programmers to follow good practices of using composition instead of inheritance.
One way to mix a module into a class is the INCLUDE directive.
This directive causes the module to be included in the inheritance chain of objects just before the parent class. This means that when a method is called on a class and is not defined directly in it, it will be called from the included module. The order of inclusion matters. It cascades down similarly to styles in CSS. If several included modules contain a method with the same name, the method from the module included last will be called.
Here's a great example: the UserRepository
class with the included MongoHelper
module can use the methods contained in the module as if they were its own class or instance methods.