RbRuby · Lesson 8 of 11
Modules & Mixins
Ruby solves the multiple-inheritance problem with modules. You `include` a module and its methods become instance methods. `extend` makes them class methods. This is called a mixin and it's genuinely elegant.
✦ Tip
The Comparable and Enumerable modules are two of Ruby's most powerful standard mixins. If your class represents ordered values, include Comparable and define `<=>`. If it contains a collection, include Enumerable and define `each`. You instantly get dozens of methods for free.
The Comparable and Enumerable modules are two of Ruby's most powerful standard mixins. If your class represents ordered values, include Comparable and define `<=>`. If it contains a collection, include Enumerable and define `each`. You instantly get dozens of methods for free.