Modules
Modules
Modules serve two purposes in Ruby, namespacing and mix-in functionality.
A namespace can be used to organize code by package or functionality that separates common names from interference by other packages. For example, the IRB namespace provides functionality for irb that prevents a collision for the common name “Context”.
Mix-in functionality allows sharing common methods across multiple classes or modules. Ruby comes with the Enumerable mix-in module which provides many enumeration methods based on the each
method and Comparable allows comparison of objects based on the <=>
comparison method.
Note that there are many similarities between modules and classes. Besides the ability to mix-in a module, the description of modules below also applies to classes.