Modules and Mixins

Lesson 13 of 20

Bundle reusable methods into a module and mix them into any class with include — sharing behavior without inheritance.

The big picture

Bundles of behavior

A module is a named bag of methods and constants. Mix it into a class and every instance gains those methods — no family tree required.

Modules Hold Code

module Name ... end groups methods and constants under one name.

include Mixes In

include Greetable inside a class makes every instance method of the module available.

Many Modules, One Class

A class can include several modules — behavior composes like building blocks.

Famous Mixins

Comparable and Enumerable power <, ==, map and select across all of Ruby.