Modules

Now that you've internalized the idea that the fundamental unit of a Ruby program is the class, you might be interested to learn that classes are merely one variety of something called a module.

That's the attention-grabbing way to put it. In fact, classes really are the fundamental unit of object-oriented design, and therefore of programming Ruby. But it is also true that classes are a type of module. Class Class is a subclass of class Module. That alone puts modules front and center in the Ruby language world.

Not all modules are classes, though. Modules have traits of their own that figure very prominently and powerfully in Ruby program design.

A module looks a lot like a class:

 module MyFirstModule def say_something puts "Hello from ...

Get Sams Teach Yourself Ruby in 21 Days now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.