Designing Modules and Classes for Clarity and Reuse

When writing a program in Ruby, you organize your code into classes and modules. As we've seen, your efforts have to mesh with certain constraints in the way Ruby organizes and finds code at the class or module level and also at the disk file level.

But even in cases where it's going to be self-evident what to call the files and where to put them, you still have to make decisions about class and module hierarchies. A lot of that decision making has to do with deciding what should be a class and what should be a module.

Classes and modules are very closely related. In almost any case you could, if you felt like it, cut methods from a class, paste them into a module, and then mix the module into ...

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.