Embedded Modules and Namespace Management

Most of our module examples have involved what might be called the classic or “bread-and-butter” usage of modules in Ruby: A class mixes in a module with an include statement, probably at the very beginning of the class definition, and that module's methods and class variables become accessible to instances of the class that's doing the mixing in.

That, along with inheritance and a few other techniques, is enough to get you streamlining your program design nicely. However, there's somewhat more variety to how classes and modules can interact.

Remember how definitions of modules and classes can include methods, constants, and variables (of various kinds)? Well, if you liked that, you'll love this: Definitions ...

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.