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.
Make yourself useful.