Ruby Mixins - Extend

12 marca 2025
ruby

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 EXTEND directive. This directive causes the module to be included in the inheritance chain of objects as a singleton. This means that the methods contained in the module are available as static class methods.

The Database adapter, by extending with the DatabaseHelper module, will be able to clear the database without needing to create an adapter instance by using clear_database as a static method.