Short answer :
Dir['/path/*.rb'].each { |file| require file }
Explanation :
Dir[]
is an equivalent of calling Dir.glob()
which take a pattern string and returns an array containing the matching filenames. (cf. more detail about patterns https://ruby-doc.org/core-3.1.1/Dir.html#glob-method)
Exemple with a given directory myDirectory
at filesystem root containing only file1.rb
and file2.rb
, Dir['/myDirectory/*.rb']
will return :
[
'/myDirectory/file1.rb',
'/myDirectory/file2.rb'
]
Knowing how Dir.glob
works, we just need to iterate on the list of files by passing their names in the require
method :
Dir['/myDirectory/*.rb'].each { |file_name| require file_name }