In Ruby I can require all class files in a directory at once:
Dir["/path/to/directory/*.rb"].each {|file| require file }
But what if the classes have inheritance relationships? How to avoid requiring the subclass before superclass? (or it’s impossible?)
Actually there is a dirty hack, suppose you’ve named all your files correspond to class names. But this approach works only if you have just inheritance relationships between different classes. Method overriding multiple times is not taken into account.
Let’s say you have two rb files in the to be required directory: engineer.rb and human.rb.
— engineer.rb
— human.rb
And in another file which is gonna require them, if you specify engineer first you’ll get an error:
So we can simply play with the powerful weapon autoload:
Basically autoload will require file on demand, thus the sequence of requiring is no more a problem.
If you want to know more about autoload, please check here:
http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-autoload
http://www.rubyinside.com/ruby-techniques-revealed-autoload-1652.html
But please be aware that autoload will be dead soon (because it’s not thread safe), please refer to here (by Matz):
http://www.ruby-forum.com/topic/3036681