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


































