Modules, Classes, Pterosaurs

Flatiron School / 21 October 2013

The following is a guest post by Ivan Brennan and originally appeared on his blog. Ivan is currently in the Ruby-003 class at The Flatiron School. You can follow him on Twitter here.

Pterosaurs, bats, and birds can/could all fly, but each evolved the ability independent of the others. This is an example of convergent evolution, the “independent evolution of similar features in species of different lineages”. The wings of bats, pterosaurs, and birds, the body-plans of marsupials and mammals, and the eyes of vertebrates and cephalopods are just some examples of this.

In Ruby, classes allow objects to be arranged into a hierarchical lineage, using inheritence to pass features from a parent class to all it’s descendants. But we often want to share functionality across unrelated classes. Modules allow us to do just that. Let’s consider the case of pterosaurs and bats.

Contrast this with sharing functionality via classes. Let’s create a couple species of bat by defining descendants of the Bat class:

Both sam and candice can echolocate and fly, since the SeminoleBat and CanyonBat classes are descendants of the Bat class. The SeminoleBat and CanyonBat classes also each have unique functionality that distinguishes them, and is not shared between their two classes. Only instances of SeminoleBat can migrate, and only those of CanyonBat can hibernate.

And while Pterosaur, like Bat and all its descendants, can fly, it can’t echolocate, migrate, or hibernate.

There is a bit more to Modules (they can be used to define class methods as well as instance methods, and they can include constants), but the general purpose they serve is to enable sharing functionality across unrelated classes.

What Happens in Method, STAYS in Method! Previous Post Variable Scope: How I Learned to Stop Worrying and Love the Ruling Class Next Post