Something very cool in ruby are mixins. As the word says mixin is a technique to mixed a module into a class using the statement “include”. While including a module is a very large topic I focused on the opportunity to include module defined in the Ruby standard library into your class to take advantages of the methods and abilities that these modules provide. The Comparable module in ruby define methods like < <= == > >= between? . What if I want to achieve is making my class instances comparable so that I can ask to ruby if class_a > class_b. Here an example: <div></div> The Person class include the module Comparable and implements one single method (<=>). This methods is used by the Comparable module to perform the logic of any of the operator that the module provide.In the example I say to the Person class to compare instances through the @name attribute. But we even gain more functionalities. Once that our classes are able to be compared they are also able to be sorted if placed inside an array. Amazing!!!
But what if I want to find Persons by name?? Wen can use for this purpose the Enumerable module. What we can do is to create a custom PersonEnumerator class like this:
Including the Enumerable module and implementing the each method we gain most of the many methods that the module expose. In our example I find a person object passing a block that filters each Person by name.
The mixin technique applied to the ruby standard library is very powerfull. Your code code gets more elegant and more Ruby Way for you to maintain and for your collaborator to read and understand
Ruby amazes me any day more!