Modules & Mixins
Ruby is a dynamic, beginner-friendly programming language, and modules let you both namespace your code and share behaviour across classes through mixins — Ruby's elegant alternative to multiple inheritance.
Learn Modules & Mixins in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll create modules, use them as namespaces, and mix shared methods into classes with include .
What You'll Learn in This Lesson
1️⃣ Modules as Namespaces and Toolboxes
A module groups related constants and methods. You can call its self. methods directly and reach inside with the :: operator. As a namespace , a module lets two libraries each define a Track without colliding.
2️⃣ Mixins: Sharing Behaviour with include
The real power is the mixin . Define instance methods in a module, then include it in any class to give every instance those methods. A class can include many modules. Ruby's own Comparable mixin turns a single <=> method into a full set of comparison operators.
Your turn. Give a Duck both walk and swim by mixing in two modules. Complete the TODO s, then run it.
Write one Describable module and share it between two unrelated classes — the essence of a mixin. Run with ruby describable.rb .
📋 Quick Reference — Modules & Mixins
Practice quiz
What is a key difference between a module and a class?
- modules are faster
- a class cannot have methods
- a module cannot be instantiated with .new
- modules cannot hold constants
Answer: a module cannot be instantiated with .new. You cannot call .new on a module; modules namespace or mix in behaviour.
Which operator reaches a constant inside a module, like PI?
- double colon, MathTools::PI
- a dot, MathTools.PI
- an arrow, MathTools->PI
- a hash, MathTools#PI
Answer: double colon, MathTools::PI. Use :: to reach module constants, e.g. MathTools::PI.
What does include do?
- adds class methods
- creates a subclass
- deletes methods
- adds the module's instance methods to the class
Answer: adds the module's instance methods to the class. include mixes a module's instance methods into the class.
How many modules can a single class include?
- zero
- as many as you like
- exactly one
- exactly two
Answer: as many as you like. A class can compose many mixins via multiple include statements.
What does extend add to a class instead of include?
- class methods
- instance methods
- constants
- nothing
Answer: class methods. extend adds the module's methods as class (singleton) methods.
Including Comparable requires you to define which method?
- each
- to_s
- <=>
- compare
Answer: <=>. Comparable builds <, >, == and more on top of your <=> method.
Including Comparable and defining <=> gives you which methods for free?
- map and select
- <, >, ==, between?, clamp
- push and pop
- each and count
Answer: <, >, ==, between?, clamp. Comparable derives the full comparison set from <=>.
How does a mixin differ from inheritance?
- mixins are single, inheritance is multiple
- they are identical
- mixins require a parent class
- inheritance is single (one superclass); mixins can be many
Answer: inheritance is single (one superclass); mixins can be many. A class has one superclass but can include many modules.
A mixin method like greet that uses name requires that:
- name is a global variable
- the including class provides a name method
- name is a constant
- you call extend instead
Answer: the including class provides a name method. The mixin relies on the host class supplying the name method.
Which call shows the method-lookup order including mixins?
- Klass.methods
- Klass.parents
- Klass.ancestors
- Klass.lookup
Answer: Klass.ancestors. ancestors lists the lookup chain, including included modules.