Ruby Academy · Lesson

Mixins and Modules

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

Lesson 4 of 410 steps

Mixins and Modules is a free Ruby Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.

Sharing Code with Modules

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

Defining a Module

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

module Greetings
  def say_hello
    puts 'Hello!'
  end
end

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

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

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

Using Constants in Modules

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

module MathOperations
  PI = 3.14
end

puts MathOperations::PI

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')

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

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
Free to start

Learn Ruby with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
29
Lessons
109

Frequently Asked Questions

Is the “Mixins and Modules” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “Mixins and Modules”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

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