Last Updated: February 25, 2016
·
10.97K
· andy_kif

Require all files within a directory in Ruby

Have you ever needed to load all files from within a directory in Ruby ?
Here's a snippet showing how to do it.

# requires all files recursively inside a directory from current dir
# @param _dir can be relative path like '/lib' or "../lib"
def require_all(_dir)
    Dir[File.expand_path(File.join(File.dirname(File.absolute_path(__FILE__)), _dir)) + "/**/*.rb"].each do |file|
        require file
    end
end

Have a nice day :)