Project

General

Profile

Actions

Feature #14927

open

Loading multiple files at once

Added by Anonymous over 6 years ago. Updated 8 months ago.

Status:
Assigned
Target version:
-
[ruby-core:88027]

Description

Just a proof concept I wanted to share. Maybe it could be useful?

Say you want to load all the .rb files in your lib directory:


Dir['lib/**/*.rb'].each { |file| load(file) }

This approach may not work if your files have dependencies like that:

# lib/foo.rb

class Foo < Bar

end
# lib/bar.rb

class Bar

end

Foo class needs Bar class. You will get a NameError (uninitialized constant Bar).

So in my personal projects, I use this algorithm to load all my files and to automatically take care of dependencies (class/include):

def boot(files)
  i = 0
  while i < files.length
    begin
      load(files[i])
    rescue NameError
      i += 1
    else
      while i > 0
        files.push(files.shift)
        i -= 1
      end
      files.shift
    end
  end
end

boot Dir['lib/**/*.rb'] # It works! foo.rb and bar.rb are properly loaded.

My point is: it would be cool if Kernel#load could receive an array of filenames (to load all these files in the proper order). So we could load all our libs with just a single line:

load Dir['{path1,path2}/**/*.rb']

Related issues 2 (2 open0 closed)

Related to Ruby master - Feature #12858: Supporting batch-requiring of files in rubyOpenActions
Related to Ruby master - Feature #7121: Extending the use of `require'Assignedmatz (Yukihiro Matsumoto)Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0