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 :)
Written by Andy Kifer
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#