I am working on an API that needs to load all of the .rb files in its current directory and all subdirectories. Currently, I am entering a new require statement for each file that I add but I would like to make it where I only have to place the file in one of the subdirectories and have it automatically added.
Is there a standard command to do this?
like Miguel Fonseca said, but in ruby >= 2 you can do :
I use the gem require_all all the time, and it gets the job done with the following pattern in your requires:
This should recursively
load
all .rb files in the directory specified bydir
. For example,rLoad Dir.pwd
would work on the current working directory.Be careful doing this, though. This does a depth-first search and if there are any conflicting definitions in your Ruby scripts, they may be resolved in some non-obvious manner (alphabetical by folder/file name I believe).
In this case its loading all the files under the lib directory:
This will recursively load each
.rb
file.You should have a look at this gem. It is quite small so you can actually re-use the code instead of installing the whole gem.