Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple classes in one file, Ruby Style Question

Tags:

ruby

styles

I am writing a script that takes data from a database and creates GoogleChart URLs from the parsed data. I only need to create two type of charts, Pie and Bar, so is it wrong if I stick both of those classes in the same file just to keep the number of files I have low?

Thanks

like image 486
Hunter McMillen Avatar asked Jul 21 '11 13:07

Hunter McMillen


People also ask

Is Ruby code written in one file or multiple files?

If you look at any open source Ruby projects online, you'll quickly notice that their code isn't all written in one file. In fact, programmers generally try to reduce the length of the files they are writing by splitting their code up into multiple files.

What is this Ruby style guide for?

This Ruby style guide recommends best practices so that real-world Ruby programmers can write code that can be maintained by other real-world Ruby programmers.

How do I split a Ruby program into multiple files?

So, In order to split your program's logic up across multiple files, Ruby starts with the file that starts your program up, and then uses one of these ways of finding the other files in your program. The first one of these we see in active_record.rb is "require", so let's see how that works first.

When to use a class instead of a module?

Classes should be used only when it makes sense to create instances out of them. Prefer the use of module_function over extend self when you want to turn a module’s instance methods into class methods. When designing class hierarchies make sure that they conform to the Liskov Substitution Principle. Try to make your classes as SOLID as possible.


2 Answers

If you're asking the "ruby" way, then it is to put your classes in separate files. As some others have alluded to, placing your classes in separate files scales better. If you place multiple classes in the same file and they start to grow, then you're going to need to separate them later.

So why not have them separate from the beginning?

UPDATE

I should also mention that autoload works by expecting classes to be in their own files. For instance, if you're in a Rails environment and you don't separate classes into different files, you'll have to require the file explicitly. The best place to do this would be in the application.rb file. I know you're not in a Rails environment, but for others who may find this answer, maybe this info will be helpful.

UPDATE2

By 'autoload', I meant Rails autoload. If you configure your own autoload, you can put classes in the same file; but again, why? The Ruby and Java communities usually expect classes to be in separate files. The only exception are nested classes, but that's for more advanced design patterns.

like image 146
WattsInABox Avatar answered Oct 05 '22 10:10

WattsInABox


Usually more less-complex files are better than less more-complex ones. Specially if you need to share the code with others.

like image 44
Rafael Barbosa Avatar answered Oct 05 '22 10:10

Rafael Barbosa