include and extend
Mixing in behavior.
include and extend is a free Ruby Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Mixing In Behavior
The real power of modules is the mixin: sharing methods across classes without inheritance. The two main tools are include and extend.
module Walkable
def walk
"#{name} is walking"
end
end
class Person
include Walkable
attr_reader :name
def initialize(name)
@name = name
end
end
puts Person.new("Ada").walkinclude Adds Instance Methods
include mixes a module's methods in as instance methods. Every object of the class gains them.
module Sayable
def say(msg)
"#{msg}!"
end
end
class Dog
include Sayable
end
puts Dog.new.say("Woof")Reuse Across Classes
One module can be included by many unrelated classes, sharing behavior without a common parent.
module Timestamped
def stamp
"created"
end
end
class Post; include Timestamped; end
class Comment; include Timestamped; end
puts Post.new.stamp
puts Comment.new.stampextend Adds Class Methods
extend mixes a module's methods in as class methods (methods on the class object itself).
module Findable
def find(id)
"Finding record #{id}"
end
end
class User
extend Findable
end
puts User.find(42)extend on an Object
You can extend a single object so only it gains the methods, leaving others untouched.
module Admin
def privileges
"all access"
end
end
user = Object.new
user.extend(Admin)
puts user.privilegesinclude vs extend
Remember the distinction:
include=> methods become instance methods.extend=> methods become class/object methods.
module Helper
def help; "helping"; end
end
class A; include Helper; end
class B; extend Helper; end
puts A.new.help
puts B.helpModules See Host Methods
A mixed-in method runs in the context of the host object, so it can call the host's own methods and access its data.
module Describable
def describe
"I am #{role}"
end
end
class Worker
include Describable
def role; "a worker"; end
end
puts Worker.new.describeThe included Hook
Define self.included(base) to run code automatically when a module is included. A classic pattern adds class methods at the same time.
module Trackable
def self.included(base)
puts "Trackable added to #{base}"
end
end
class Order
include Trackable
endChecking Membership
is_a? and ancestors reveal that an included module becomes part of the object's type chain.
module Flyable; end
class Bird; include Flyable; end
b = Bird.new
puts b.is_a?(Flyable)
p Bird.ancestorsCombining Several Mixins
A class can include multiple modules, composing behavior from many small, focused pieces.
module Swimmable; def swim; "swimming"; end; end
module Runnable; def run; "running"; end; end
class Athlete
include Swimmable
include Runnable
end
a = Athlete.new
puts a.swim
puts a.runPutting It Together
Mixins share behavior flexibly:
includefor instance methods.extendfor class or single-object methods.- Mixed methods run in the host's context.
- Compose many small modules.
module Greet
def hi; "hi from #{self.class}"; end
end
class Cat; include Greet; end
puts Cat.new.hiQuick Check
Test your understanding of include and extend.
Recap
You learned mixins:
include=> instance methods.extend=> class or single-object methods.- Mixed methods run in the host context.
- The
includedhook fires on inclusion.
module Loud
def shout(s); s.upcase; end
end
class Speaker; include Loud; end
puts Speaker.new.shout("hello")Frequently asked questions
Is the “include and extend” lesson free?
Yes — the full text of “include and extend” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
What will I learn in “include and extend”?
Mixing in behavior. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “include and extend” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Defining Modules
- include and extend
- Comparable and Enumerable
- prepend and Method Resolution