Organizing Your Source Code

We have two related problems to solve: how do we split our source code into separate files, and where in the file system do we put those files? Some languages, such as Java, make this easy. They dictate that each outer-level class should be in its own file and that file should be named according to the name of the class. Other languages, such as Ruby, have no rules that relate source files and their content. In Ruby, you’re free to organize your code as you like.

That said, you’ll find that some kind of consistency helps. It’ll make it easier for you to navigate your own projects, and it’ll also help when you read (or incorporate) other people’s code.

The Ruby community has largely adopted a de facto standard. In ...

Get Programming Ruby 3.3 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.