hero-background-image-header

Modules, Classes, Pterosaurs

By Flatiron SchoolOctober 21, 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.

Blog post image: tumblr_inline_mv0wr1bs1i1rtan47.jpg

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.

Blog post image: tumblr_inline_mv0wrmyBDK1rtan47.png

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

Blog post image: tumblr_inline_mv0ws6XFZw1rtan47.png

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.

Blog post image: tumblr_inline_mv0wsr0hwR1rtan47.png

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

Blog post image: tumblr_inline_mv0wtc3Qil1rtan47.png

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.

Headshot of Flatiron School

Flatiron School

Blog Post Author

All Articles by Flatiron School