0Pricing
Ruby Academy · Lesson

Mixins and Modules

Use modules and mixins to extend class functionality without traditional inheritance.

Mixins and Modules is a free Ruby Academy lesson on CoddyKit — lesson 4 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.

1

Mixins and Modules

Modules allow code organization and reuse without traditional inheritance.

Mixins enable multiple classes to share behavior without being directly related.

In this lesson, you will learn how to use modules and mixins in Ruby.

Mixins and Modules — illustration 1

2

Defining a Module

Modules are defined using the module keyword and cannot be instantiated.

module Greetings
  def say_hello
    puts 'Hello!'
  end
end

3

Including a Module

Use include to add module methods as instance methods.

module Greetings
  def say_hello
    puts 'Hello!'
  end
end

class Person
  include Greetings
end

person1 = Person.new
person1.say_hello

4

Using Extend

extend adds module methods as class methods.

module Greetings
  def say_hello
    puts 'Hello!'
  end
end

class Person
  extend Greetings
end

Person.say_hello

5

Using Multiple Modules

A class can include multiple modules for different functionalities.

module Walkable
  def walk
    puts 'Walking...'
  end
end

module Talkable
  def talk
    puts 'Talking...'
  end
end

class Person
  include Walkable
  include Talkable
end

person1 = Person.new
person1.walk
person1.talk

6

Using Constants in Modules

Modules can store constants that can be accessed using ::.

module MathOperations
  PI = 3.14
end

puts MathOperations::PI

7

Encapsulating Methods in Modules

Modules allow defining helper methods that can be used across different classes.

module Formatter
  def format_text(text)
    text.upcase
  end
end

class Document
  include Formatter
end

doc = Document.new
puts doc.format_text('hello')

8

9

Using Self in Modules

Methods inside a module can be defined using self to make them accessible directly.

module Helper
  def self.greet
    puts 'Hello from a module!'
  end
end

Helper.greet

10

Congratulations!

You have learned how to use mixins and modules in Ruby.

Next, you will explore working with files and input/output operations.

Mixins and Modules — illustration 10

Frequently asked questions

Is the “Mixins and Modules” lesson free?

Yes — the full text of “Mixins and Modules” 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 “Mixins and Modules”?

Use modules and mixins to extend class functionality without traditional inheritance. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Mixins and Modules” 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

  1. Classes and Objects
  2. Instance Variables and Methods
  3. Inheritance and Polymorphism
  4. Mixins and Modules
← Back to Ruby Academy