Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Ruby Dictionary

  1. Home
  2. Ruby Dictionary
  3. module / include / extend / mixin

module / include / extend / mixin

Ruby's module and mixin system. Modules are used as an alternative to multiple inheritance, allowing multiple classes to share common functionality.

Syntax

# Define a module.
module ModuleName
  def instance_method
    # processing
  end

  def self.module_function
    # processing
  end
end

# include: adds module methods as instance methods.
class ClassName
  include ModuleName
end

# extend: adds module methods as class methods.
class ClassName
  extend ModuleName
end

# prepend: inserts the module before the class in the method lookup chain (useful for wrapping methods).
class ClassName
  prepend ModuleName
end

# extend a module onto a specific object only (singleton method).
object.extend(ModuleName)

Method List

Syntax / MethodDescription
module ModuleNameDefines a module. Modules cannot be instantiated, and unlike classes, they cannot be inherited.
include ModuleNameAdds the module's methods as instance methods of the class.
extend ModuleNameAdds the module's methods as class methods of the class.
prepend ModuleNameInserts the module at the front of the method lookup chain (useful for preprocessing before overrides).
include? / ancestorsLets you check which modules a class has included.

Sample Code

# A module that provides greeting functionality.
module Greetable
  def greet
    "Hello! I am #{name}."
  end

  def farewell
    "Goodbye! That was #{name}."
  end
end

# A module that provides logging functionality.
module Loggable
  def log(message)
    puts "[LOG] #{self.class}: #{message}"
  end
end

# Add module methods as instance methods via include.
class Person
  include Greetable
  include Loggable

  attr_reader :name

  def initialize(name)
    @name = name
  end
end

class Robot
  include Greetable

  attr_reader :name

  def initialize(name)
    @name = name
  end
end

# Module methods are now available.
p = Person.new("Alice")
puts p.greet    # Hello! I am Alice.
puts p.farewell  # Goodbye! That was Alice.
p.log("Started")  # [LOG] Person: Started

r = Robot.new("R2D2")
puts r.greet    # Hello! I am R2D2.

# Inspect the module ancestry.
puts Person.ancestors.inspect
# [Person, Loggable, Greetable, Object, Kernel, BasicObject]

Overview

Ruby does not support multiple inheritance, but mixins allow multiple classes to share common functionality through modules. A module is defined separately from a class as a "collection of features" and mixed into the classes that need it using include or extend.

include adds the module's methods as instance methods, while extend adds them as class methods. You can check the method lookup order using ancestors. Modules included later appear earlier in the lookup order (stack structure).

Modules cannot be instantiated. Giving a module instance variables can cause confusion because they mix with the instance variables of the including class. It is recommended to define only stateless methods in a module.

For custom comparisons using the standard library's Comparable module, see Comparable / <=>. For how inheritance works, see Inheritance / super.

If you find any errors or copyright issues, please .