From: "Iñaki Baz Castillo" Date: 2009-04-04T06:59:37+09:00 Subject: Re: Loading classes in order El Viernes 03 Abril 2009, Elias Orozco escribió: > Iñaki Baz Castillo wrote: > > El Viernes 03 Abril 2009, Elias Orozco escribió: > >> I haven't require yet. So can anyone help or give me any hint on how to > >> load them all dynamically and deal with those dependencies? > > > > path = File.dirname(__FILE__) + "/files_dir" > > > > require "#{path}/required_file1" > > require "#{path}/required_file2" > > require "#{path}/required_file3" > > > > Dir.chdir(path) > > Dir["*.rb"].each do |file| > > require "#{path}/#{file}" > > end > > Thanks Inaki, but in that example I will have to specify the require > files and that's exactly what I don't want. A lot of classes will be > copied into that folder and I can't hard-code all those requires. When failing due to a non existing class, Ruby raises a NameError exception. Then you can add something as: --------------------------- failed_files=[] path = xxxxxx Dir.chdir(path) Dir["*.rb"].each do |file| begin require "#{path}/#{file}" rescue NameError failed_files << file end end failed_files.each do |file| require "#{path}/#{file}" end ---------------------------- (It should be very improved however). -- Iñaki Baz Castillo