Issue #14927 has been updated by marcandre (Marc-Andre Lafortune).


nobu (Nobuyoshi Nakada) wrote:
> Doesn't the order matter?

Very often, it does not. If it does, one can always require the one that's needed first, say, then require the whole directory; require won't load the same file twice so this works fine.

We wrote a small method for this in `deep-cover`: https://github.com/deep-cover/deep-cover/blob/master/core_gem/lib/deep_cover/tools/require_relative_dir.rb

One thing I really like about it is that it makes it clear that the whole directory is loaded. If there's a missing `require "fpm/package/something"` in the list above, it can take a while to notice.


My opinion on the feature request: not great for `load`, but could be useful for multiple `require_relative`.

----------------------------------------
Feature #14927: Loading multiple files at once
https://bugs.ruby-lang.org/issues/14927#change-73951

* Author: deneb (Sbastien Durand)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
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:

~~~ ruby

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


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

~~~ ruby
# lib/foo.rb

class Foo < Bar

end
~~~

~~~ ruby
# 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):

~~~ ruby
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
~~~
~~~ ruby

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:

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




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>