Class-Module Distribution Across Program Files

When it comes to deciding which classes and modules to put in what files, Ruby allows all sorts of permutations. You have to consider what you, and perhaps other people, might want to do with your code later.

One option that you have is to put each class in a different file. You can also group more than one class in one file, if you want.

There's a convention in other object-oriented languages of creating one file for every class defined. When you write something in Java, for example, you end up with many files, each containing the code for a single class.

This practice is less strictly followed in Ruby; you'll often see a program source file with more than one class definition in it. When it comes ...

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.