Wednesday, February 11, 2009

Ruby: Require All Files In A Directory

In my Rails app, I have an external program that I keep in my lib directory for offline processing. Within that program are several ruby classes that I'd like to load at startup. Rather than adding require statements for each one and running the risk of forgetting one, you can load the whole directory with the following line of code:


Dir.glob(File.join(File.dirname(__FILE__), 'images/*.rb')).each {|f| require f }


where, in my case, I'm loading all of the *.rb files in my lib/images folder. Now, I can add new files to that directory and they will automatically get loaded at during startup of my external program.

See this link for more information.

No comments: