Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Build an Address Book in Ruby Class Design Phone Number Class

Why not put both classes in the same file?

Is it feasible/will it work with both "contact" and "phone number" classes in the same .rb file? Why separate them in the first place? Ultimately, can a ruby programme consist of just one large, well-organised file?

3 Answers

The answer to your question is 'yes, it can'. However, it's considered 'best practice' to separate the classes out into their own files. There are two main reasons for this: one is that classes often (but not always) become quite large, so it's for general readability and organisation of your code.

Secondly, and this is also related to the first reason: a lot of thought and design principles go into organisation of classes in any program - careful thought goes into what classes should be and what those classes should be responsible for. It then feels natural to have files in separate classes.

So, two main, but interrelated reasons for separating them out.

For further reading, google 'SRP' or 'Single Responsibility Principle'.

Rachelle Wood
Rachelle Wood
15,362 Points

This is an important concept too if you ever want to go to another object-oriented language like Java that actually requires you to make a separate file for each class. Great explanation!

Clear and precise answer, thanks very much Andrew.

But if you have a number of related classes, why not place them in a single file (module)? Isn't that better than having to keep track of several .rb files, especially when you want to create an application and deploy it? Thanks