liveBook · Manning

Module

As explained in The Well-Grounded Rubyist, Fourth Edition

A module is a Ruby construct that groups constants, methods, and other definitions under a namespace and can provide reusable behavior.

Suppose several kinds of objects need the same behavior, but do not belong under the same superclass. Define that behavior in a module and mix it into each class that needs it. A module bundles methods and constants, much like a class, but cannot be instantiated. section 4.0 section 4.4.1

Add behavior to instances

Define `module Greeting; def greet; puts "Hello!"; end; end`, then write `class Greeter; include Greeting; end`. A `Greeter` instance can call `greet` even though the method is defined outside its class. `include` makes the module part of the instances’ method-lookup path; it does not create an instance of `Greeting`. section 4.1 section 4.5

The way a module is added determines who gains access to its methods:

  • `include` gives instances access to the module’s instance methods. Unlike inheritance, a class can mix in more than one module. section 4.1 section 3.5.1
  • `extend` gives a particular object access to those methods. Inside a class definition, that object is the class itself, so the methods can be called on the class rather than its instances. section 4.2.3
  • `prepend` is another way to add a module to a lookup path. An `included` or `prepended` callback can respond when the corresponding operation occurs. section 4.1 section 15.1.2

Choose a class for something that should have instances; consider a module for a characteristic shared across kinds of objects. For example, stacklike operations can be defined once and mixed into classes that need last-in, first-out behavior. A module can also serve as a namespace: `Tools::Hammer` and `Piano::Hammer` can name different classes without a collision. The `::` notation alone does not reveal whether a nested name denotes a class, module, or constant. section 4.1.1 section 4.4.1 section 4.4.2

Listing 4.2 Mixing the Stacklike module into the Stack class
require_relative "stacklike"   1
class Stack
  include Stacklike      2
end

The Stack example separates loading the file that defines a module (`require_relative "stacklike"`) from mixing that module into a class (`include Stacklike`). The first locates code on disk; the second works with a module already available in the program. section 4.1.2

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Works through definition, inclusion, lookup, extension, namespaces, and inclusion callbacks, while treating the mechanics of extend only preliminarily.
Examples
Uses a greeting mixin, stacklike behavior, temperature conversion, nested class names, and an inclusion callback.
What it assumes
Assumes familiarity with objects, classes, methods, and inheritance; teaches module definition and mixing from their basic syntax.
What you can do after
Build it from scratch: define reusable Ruby behavior, mix it into classes, and use modules to separate namespaces.
How it connects
Connects modules to classes, single inheritance, mixins, method lookup, class methods, callbacks, and namespaces.

Also explained in

Compare how 35 books explain module →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.