Question
I recently moved from Java to Ruby, and one language feature I am not yet familiar with is the module.
What exactly is a module in Ruby? When should you use a module, and why would you choose a module instead of a class?
Short Answer
By the end of this page, you will understand what Ruby modules are, how they differ from classes, and the two main reasons developers use them: sharing behavior and organizing code with namespaces. You will also see when a class is the right choice, when a module is better, and how both are often used together in real Ruby codebases.
Concept
In Ruby, a class and a module are both ways to group code, but they serve different purposes.
A class is mainly used to create objects. It acts like a blueprint for instances:
class User
def initialize(name)
@name = name
end
end
user = User.new("Ava")
A module cannot be instantiated. You cannot call .new on a module. Instead, modules are mainly used for two things:
- Mixing in shared behavior
- Creating namespaces
1. Shared behavior
A module can contain methods that multiple classes can reuse.
module Speakable
def speak
"Hello"
end
end
class Person
include Speakable
end
class Robot
include Speakable
end
Now both Person and Robot have a speak method, without needing inheritance between them.
2. Namespaces
A module can group related classes and constants to avoid name conflicts.
module Payments
class Processor
end
end
module Admin
class Processor
end
end
Here, Payments::Processor and Admin::Processor are two different classes with the same class name.
Why this matters
If Ruby only had classes, developers might overuse inheritance just to share methods. Modules give Ruby a more flexible way to reuse code without forcing an “is-a” relationship.
For example:
- A
Dogis aAnimal→ inheritance may make sense. - A
Dogcan be trained and can be tracked → modules may make more sense.
So the key idea is:
- Use a class for objects and state.
- Use a module for shared behavior and namespacing.
Mental Model
Think of a class as a factory blueprint and a module as a toolbox or a folder.
- A class is a blueprint that lets you build objects.
- A module is a toolbox of methods you can attach to classes, or a folder that keeps related code together.
Toolbox analogy
Imagine several workers:
- A carpenter
- An electrician
- A plumber
They are different types of workers, so they should not all inherit from one another. But they may all use the same safety procedures. Instead of forcing them into a strange inheritance chain, you put those shared procedures in a module and include it where needed.
Folder analogy
Now imagine you have files named Report in different departments:
Sales::ReportFinance::Report
The module acts like a folder name that keeps things organized and prevents collisions.
That is the mental model:
- Class = blueprint for making things
- Module = shared toolbox or organizing folder
Syntax and Examples
Basic class syntax
class User
def initialize(name)
@name = name
end
def greeting
"Hello, #{@name}!"
end
end
user = User.new("Mina")
puts user.greeting
A class:
- can be instantiated with
.new - can store instance data in variables like
@name - is used to model objects
Basic module syntax
module Greeting
def say_hello
"Hello!"
end
end
A module:
- cannot be instantiated
- is often included in classes
- is useful for shared methods or namespacing
Using a module with include
Step by Step Execution
Consider this example:
module Walkable
def walk
"Walking"
end
end
class Person
include Walkable
end
person = Person.new
puts person.walk
Step-by-step
- Ruby reads the
Walkablemodule. - Inside the module, it finds a method called
walk. - Ruby reads the
Personclass. include Walkabletells Ruby to make the module's instance methods available inPerson.Person.newcreates a newPersonobject.person.walkis called.- Ruby looks for
walkon thePersoninstance. - It does not find
walkdirectly in the class, so it checks included modules.
Real World Use Cases
1. Sharing common behavior across unrelated classes
module Timestamped
def formatted_time
Time.now.strftime("%Y-%m-%d")
end
end
class Order
include Timestamped
end
class Message
include Timestamped
end
Order and Message are different concepts, but both can reuse the same behavior.
2. Organizing large applications with namespaces
module Api
class Client
end
end
module Admin
class Client
end
end
This avoids naming conflicts in bigger systems.
3. Utility or helper functions
Real Codebase Usage
In real Ruby projects, modules are used in a few common patterns.
Mixins for shared behavior
Developers place reusable methods in modules and include them in multiple classes.
module Loggable
def log(message)
puts "[LOG] #{message}"
end
end
class OrderProcessor
include Loggable
def process
log("Processing order")
end
end
This avoids duplication and avoids unnecessary inheritance.
Namespacing application areas
Large apps often group code by feature or domain.
module Payments
class ChargeService
end
class RefundService
end
end
This makes class names clearer and keeps related code together.
Guard clauses and shared checks
Modules often hold reusable validation or authorization logic.
Common Mistakes
1. Trying to instantiate a module
Broken code:
module Helper
end
helper = Helper.new
This fails because modules cannot be created with .new.
Use a class if you need instances:
class Helper
end
helper = Helper.new
2. Using inheritance when a module is a better fit
Broken design:
class Animal
def log
puts "Logging"
end
end
class Car < Animal
end
A Car is not an Animal. This inheritance relationship is wrong.
Better:
module
puts
Comparisons
| Feature | Class | Module |
|---|---|---|
Can create objects with .new | Yes | No |
| Can hold instance methods | Yes | Yes |
| Can be used for inheritance | Yes | No direct instantiation/inheritance role like classes |
| Can be included in classes | No | Yes |
| Can be used as a namespace | Yes, but less commonly | Yes, very commonly |
| Best for | Modeling objects | Sharing behavior and organizing code |
Class vs module in plain language
- Use a class when you are defining a thing that will have objects.
- Use a module when you want to share abilities or group related code.
Inheritance vs mixins
Cheat Sheet
Quick rules
- Class: use to create objects
- Module: use to share behavior or create namespaces
- Modules cannot be instantiated with
.new - Use
includeto add module methods as instance methods - Use
extendto add module methods as class-level methods on the receiving object
Basic syntax
class User
end
module Greeting
def hello
"Hello"
end
end
Include a module
class Person
include Greeting
end
Person.new.hello
Extend a class with module methods
class Person
extend Greeting
end
Person.hello
FAQ
What is the main difference between a Ruby class and a module?
A class is used to create objects, while a module is used to share behavior or organize code with namespaces.
Can a module have methods in Ruby?
Yes. A module can define methods that are mixed into classes with include, or used as module-level methods with self.method_name.
Why use a module instead of inheritance in Ruby?
Use a module when multiple classes share a capability but do not have an "is-a" relationship. This avoids incorrect inheritance hierarchies.
Can you create an instance of a module?
No. Modules cannot be instantiated with .new.
When should I use include vs extend?
Use include for instance methods and extend for methods on the receiving object, often class methods.
Are modules only for helper methods?
No. They are also widely used for namespacing and organizing large codebases.
Can a class use more than one module?
Yes. A class can include multiple modules, which is one reason modules are so useful for combining behaviors.
Mini Project
Description
Build a small Ruby example for a publishing system. You will model content like articles and videos using classes, then use modules to share common behavior such as publishing and to organize the code inside a namespace. This demonstrates the two most important uses of modules: mixins and namespacing.
Goal
Create namespaced content classes that reuse shared publishing behavior through a module.
Requirements
Requirement 1 Requirement 2 Requirement 3
Keep learning
Related questions
Calling an Overridden Monkey-Patched Method in Ruby
Learn how to call the original method when monkey patching in Ruby, including alias_method patterns, examples, pitfalls, and practical usage.
Difference Between require and include in Ruby
Learn the difference between require and include in Ruby, when to load a file, and when to mix module methods into a class.
Fixing Ruby Gem Native Extension Errors: mkmf.rb Can't Find Header Files for Ruby
Learn why Ruby gem installs fail with missing ruby.h, how native extensions work, and how to fix header file errors on Linux servers.