Best way to require all files from a directory in ruby?

What's the best way to require all files from a directory in ruby ?


Solution 1:

How about:

Dir["/path/to/directory/*.rb"].each {|file| require file }

Solution 2:

If it's a directory relative to the file that does the requiring (e.g. you want to load all files in the lib directory):

Dir[File.dirname(__FILE__) + '/lib/*.rb'].each {|file| require file }

Edit: Based on comments below, an updated version:

Dir[File.join(__dir__, 'lib', '*.rb')].each { |file| require file }