Modules, Classes, Pterosaurs
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 […]
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:
candice can echolocate and fly, since the
CanyonBat classes are descendants of the
Bat class. The
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.
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.
Disclaimer: The information in this blog is current as of October 21, 2013. Current policies, offerings, procedures, and programs may differ.
The Data on Barbie, Greta Gerwig, and Best Director Snubs at the Oscars
Was Greta Gerwig snubbed for the 2024 Best Director Oscar nomination? How do you quantify the Barbenheimer effect? What are the biggest Best Director snubs in the history of the Oscars? Let’s explore how data science can help us understand some of the inner-workings of Oscar nominations.