ZetCode

Ruby include Method

last modified October 18, 2026

This tutorial explains Ruby's include method for module inclusion. The include method mixes module functionality into classes.

The include method adds a module's methods as instance methods to a class. This is Ruby's primary mixin mechanism for sharing behavior.

Included modules become part of the class's ancestor chain, so their methods are available to all instances. When several modules are included, the last one included is searched first.

Basic Module Inclusion

This simple example shows how to include a module in a class. The module's methods become available to class instances.

basic_include.rb
module Greeter
  def greet
    "Hello from module!"
  end
end

class MyClass
  include Greeter
end

obj = MyClass.new
puts obj.greet

The Greeter module's greet method becomes available to MyClass instances. The include statement makes this happen.

Multiple Module Inclusion

A class can include multiple modules. Methods are searched in reverse order of inclusion: the last module included is checked first.

multiple_includes.rb
module ModuleA
  def identify
    "From ModuleA"
  end
end

module ModuleB
  def identify
    "From ModuleB"
  end
end

class MyClass
  include ModuleA
  include ModuleB
end

obj = MyClass.new
puts obj.identify

ModuleB's method takes precedence because it was included last. Ruby searches modules in reverse inclusion order when resolving methods.

Included Modules in Ancestors

Included modules appear in a class's ancestor chain. This example demonstrates how to inspect the inheritance hierarchy.

ancestors_chain.rb
module MyModule
  def module_method
    "Module method called"
  end
end

class MyClass
  include MyModule
end

puts MyClass.ancestors.inspect
obj = MyClass.new
puts obj.module_method

The ancestors method places MyModule between MyClass and Object. Method lookup follows this chain, which is why instances can call module_method.

The included Hook

A module can define self.included to run code when it is included. The method receives the including class, which makes it possible to extend or register that class.

included_hook.rb
module Trackable
  def self.included(base)
    puts "#{base} includes #{self}"
  end

  def tracked?
    true
  end
end

class Order
  include Trackable
end

puts Order.new.tracked?

When Order includes Trackable, Ruby calls Trackable.included(Order). The hook prints the class name, and the instance method tracked? is available as usual.

Module Inclusion in Inheritance

When a class inherits from another, modules included in the superclass remain available to the subclass. This example shows that behavior.

inheritance_inclusion.rb
module SharedBehavior
  def shared_method
    "Available to all subclasses"
  end
end

class Parent
  include SharedBehavior
end

class Child < Parent
end

obj = Child.new
puts obj.shared_method

SharedBehavior is included in Parent, so it appears in Parent's ancestor chain and is also available to Child. The module is not copied into the subclass; it remains part of the chain.

Extend vs Include

While include adds instance methods, extend adds methods to a specific object; when that object is a class, they become class methods. This example contrasts both approaches.

extend_vs_include.rb
module MyMethods
  def say_hello
    "Hello!"
  end
end

class WithInclude
  include MyMethods
end

class WithExtend
  extend MyMethods
end

puts WithInclude.new.say_hello
puts WithExtend.say_hello

include adds the module's methods as instance methods of the class. extend adds them to the receiver's singleton class; when the receiver is a class, they become class methods. Both use the same module but affect different objects.

Including the Kernel Module

Ruby's Kernel module is automatically included in Object. This example includes it explicitly and calls a Kernel method.

kernel_inclusion.rb
class MyClass
  include Kernel

  def use_kernel_method
    puts "This is a Kernel method"
  end
end

obj = MyClass.new
obj.use_kernel_method

Kernel is already included in Object, so every ordinary object has methods such as puts. The explicit include Kernel above is therefore redundant but valid; it shows where those methods come from.

Conditional Module Inclusion

A module can be included conditionally at class definition time. This allows flexible behavior composition based on configuration or runtime factors.

conditional_include.rb
module AdminFeatures
  def admin_action
    "Performing admin action"
  end
end

ADMIN_ENABLED = true

class User
  include AdminFeatures if ADMIN_ENABLED
end

user = User.new
puts user.admin_action if user.respond_to?(:admin_action)

Because ADMIN_ENABLED is true, User includes AdminFeatures. The respond_to? check guards the call, so the program still runs safely if the flag is false.

Source

Ruby Module#include Documentation

This tutorial covered Ruby's include method with practical examples showing module mixing, inheritance, and method lookup behavior.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Ruby tutorials.