Modules versus classes in ruby
- Published: August 9, 2016
A Module in Ruby is a collection of methods and constants.
Classes in Ruby are first-class objects, each is an instance of class
Classes versus Modules
|instantiation||can be instantiated||Can *not* be instantiated|
|usage||object creation||Mixin facility. Provides a namespace.|
|methods||class methods and instance methods||module methods and instance methods|
|inheritance||inherits behaviour and can be base for _inheritance_||No _inheritance_|
|inclusion||cannot be included||can be included in classes and modules by using the include command (includes all instance methods as instance methods in a class/module)|
|extension||can not extend with extend command (only with inheritance)||module can extend instance by using extend command (extends given instance with singleton methods from module)|
Table source: Difference between a class and a module
Modules are about providing methods that you can use across multiple classes - think about them as "libraries" (as you would see in a Rails app).
Classes are about objects; modules are about functions.