Although classical inheritance is centered around the concept of code sharing via class-based hierarchies, modern object oriented programming languages provide many different mechanisms for code sharing. Ruby is no exception, providing four common ways to model inheritance-based relationships between objects
-
Classes provide a single-inheritance model similar to what is found in many other object oriented languages.
-
Modules provide a mechanism for modeling multiple inheritance which is easier to reason about than C++ style class inheritance but is more powerful than Java's interfaces.
-
Transparent delegation techniques make it possible for a subobject to dynamically forward messages to a superobject. This has similar effects to class/module based modeling on the subobject's contract, but preserves encapsulation between the objects.
-
Simple aggregation techniques make it possible to compose objects for the purpose of code sharing. This technique is most useful when the subobject is not meant to be a drop-in replacement for the superobject.