I have the following directory tree.
- app.rb
- folder/
- one/
- one.rb
- two/
- two.rb
I want to be able to load Ruby files in the folder/ directory, even the ones in the sub-directories. How would I do so?
I have the following directory tree.
- app.rb
- folder/
- one/
- one.rb
- two/
- two.rb
I want to be able to load Ruby files in the folder/ directory, even the ones in the sub-directories. How would I do so?
Jekyll does something similar with its plugins. Something like this should do the trick:
Dir[File.join(".", "**/*.rb")].each do |f|
require f
end
With less code, but still working on Linux, OS X and Windows:
Dir['./**/*.rb'].each{ |f| require f }
The '.'
is needed for Ruby 1.9.2 where the current directory is no longer part of the path.
Try this:
Dir.glob(File.join(".", "**", "*.rb")).each do |file|
require file
end
In my project this evaluates to ["./fixset.rb", "./spec/fixset_spec.rb", "./spec/spec_helper.rb"]
which causes my specs to run twice. Therefore, here's a tweaked version:
Dir[File.join(".", "**/*.rb")].each { |f| require f unless f[/^\.\/spec\//]}
This'll safely ignore all *.rb files in ./spec/