You’re closer than you think to a career in tech. Our grads have launched rewarding new careers — you can too.View Our Jobs Report
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.
Blog Post Author
Since we opened our doors in 2012, thousands of students have joined Flatiron School to launch new careers in tech.
Find the perfect course for you across our in-person and online programs designed to power your career change.
Connect with students and staff at meetups, lectures, and demos – on campus and online.
Have a question about our programs? Our admissions team is here to help.