Mixing it up
Ruby modules allow you to create groups of methods that you can then include or mix into any number of classes. Modules only hold behaviour, unlike classes, which hold both behaviour and state.
Since a module cannot be instantiated, there is no way for its methods to be called directly. Instead, it should be included in another class, which makes its methods available for use in instances of that class. There is, of course, more to this story, but let's keep it simple for now.
In order to include a module into a class, we use the method include
which takes one parameter - the name of a Module
.
In the example above, we have a Gym
and a Dojo
. They each have their own, distinct behaviour - preacher_curls
and tai_kyo_kyu
respectively. However, both require push_ups
, so this behaviour has been separated into a module, which is then included into both classes.