Skip to content

Instantly share code, notes, and snippets.

@practicingruby
Created February 14, 2012 17:49
Show Gist options
  • Select an option

  • Save practicingruby/1828557 to your computer and use it in GitHub Desktop.

Select an option

Save practicingruby/1828557 to your computer and use it in GitHub Desktop.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment