Modules and mixins

Contents
Define a module
Include a mixin
Extend with class methods
Modules as namespaces

Define a module

The keyword module groups methods and constants under one name. Unlike a class, a module cannot be instantiated on its own. Classes later borrow the grouped behavior through include or extend.

module Loud def shout(text) puts text.upcase end end puts Loud.class

Module

Include a mixin

Writing include inside a class mixes the module methods in as instance methods. Every instance then answers to the shared behavior. The class body stays short while the module carries the reusable part.

module Loud def shout(text) puts text.upcase end end class User include Loud end User.new.shout("hello")

HELLO

Extend with class methods

Writing extend inside a class mixes the module methods in as class methods instead. This placement suits finders, builders, and other helpers that belong to the class itself. Instances remain untouched by the mixed behavior.

module Finder def find(id) puts "found #{id}" end end class User extend Finder end User.find(7)

found 7

Modules as namespaces

Modules also fence off names: nesting classes and constants inside keeps each name scoped. A Cart defined inside Shop is addressed as Shop::Cart and never clashes with a Cart from another library.

module Shop class Cart def size 0 end end end puts Shop::Cart.new.size

0

Next: Classes and objects

Article author: Arthur Isaev

Related articles
Ruby
Ruby course: zero to hero in 50 lessons
What is Ruby and where it runs
Install Ruby and check the version
irb: the interactive shell
Your first Ruby script
Variables
Numbers
Strings
Symbols
Truthiness: only false and nil
Conditions
Loops
Arrays
Hashes
Ranges
Methods
Blocks, procs, lambdas
String tools
Files and IO
Exceptions
Modules and mixins
Classes and objects
Inheritance
Enumerables
Duck typing and core idioms
Gems and Bundler
Testing with Minitest
Debugging
Encodings
Time and date
JSON and YAML
HTTP clients
Sockets
REST APIs
puts, print and p
Metaprogramming
Enumerators
Pattern matching
Arguments: splats and kwargs
Rake tasks
Environment and CLI
Logging
CSV
ERB templates
WEBrick server
Rails in one lesson
Capstone: todo CLI
Capstone: JSON API
Performance
Modern Ruby tour
Hero roadmap: the whole course on one page
Install Ruby on Windows 11
Install Ruby on Ubuntu
Install Ruby on Rocky Linux
Install Ruby on macOS
Install Ruby on FreeBSD
Ruby in Docker
Threads and fibers

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: