Modules in Ruby

Modules are a fundamental concept in Ruby that provide a way to organize code, group functionality, and share it across different parts of your application.

Defining Modules: Link to heading

  • Modules are defined using the module keyword followed by the module name.
  • Classes and other modules can be nested within modules for better organization.

Example: Link to heading

module Model
    class Company

    class Employee

    module Reports
        class ExcelReporter
            def build
                puts "Generating excel report"

    class EmailReporter

excel_report = # Generating excel report

Namespace: Link to heading

  • Modules help create namespaces to avoid name conflicts.
  • You can access nested elements using the scope resolution operator ( :: ).

Example: Link to heading

excel_report = # Generating excel report

Including Modules: Link to heading

  • Modules can be included in classes using the include keyword.
  • This allows the class to inherit the methods defined in the module.

Example: Link to heading

class User
    # Include the `Nameable` module from the `Model` module
    include Model::Nameable

    def initialize(name)
        @name = name

    def greet
        puts "Hello, #{@name}"

Extending Modules: Link to heading

  • Modules can be extended to other objects using the extend keyword.
  • This makes the module methods available as class methods of the extended object.

Example: Link to heading

module Speakable
    # This method makes an object speak a message
    def speak(message)
        puts message

class Cat
    # Extend the `Cat` class with the `Speakable` module
    extend Speakable

    def meow
        speak "Meow!"

Mixins: Link to heading

  • Modules can be used as mixins to provide specific functionality to multiple classes.
  • This promotes code reuse and prevents code duplication.

In summary, modules offer a powerful mechanism for organizing code, promoting reusability, reducing code duplication, and avoiding naming conflicts. They are essential components for creating clean, maintainable and scalable Ruby applications.

Keep learning about modules to improve your Ruby programming skills!

Additional Tips: Link to heading

  • Use modules to group related functionality.
  • Take advantage of modules to create reusable components.
  • Employ modules to avoid naming conflicts within your code base.

Example: Link to heading

module MathHelpers
    # This method adds two numbers
    def add(x, y)
        x + y

    # This method subtracts two numbers
    def subtract(x, y)
        x - y

class Calculator
    # Include the `MathHelpers` module
    include MathHelpers

    def calculate(operation, x, y)
        send(operation, x, y)  # Llamada dinámica de métodos según la operación

calculator =
result = calculator.calculate(:add, 5, 3)
puts result # 8

Modules allow you to write modular, well-structured and maintainable Ruby programs!

<< Object Oriented Programming (OOP) in Ruby Bundler and Gems >>