Buy me a coffeeBuy me a coffee

Modules versus Classes in Ruby


A Module in Ruby is a collection of methods and constants.

Classes in Ruby are first-class objects, each is an instance of class Class.

Classes versus Modules

  Class Module
instantiation can be instantiated Can not be instantiated
usage object creation Mixin facility. Provides a namespace.
superclass module object
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.


Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.