Issue #14927 has been updated by shevegen (Robert A. Heiler).


I thought about creating a new issue but then I remembered that
the issue here refers to a similar use case that I wanted to
show.

Take the following link as an example:

https://github.com/jordansissel/fpm/blob/master/lib/fpm.rb

In the event that the project may be relocated, here is the
copy/paste outcome of that code:

    require "fpm/namespace"

    require "fpm/package"
    require "fpm/package/dir"
    require "fpm/package/gem"
    require "fpm/package/deb"
    require "fpm/package/npm"
    require "fpm/package/rpm"
    require "fpm/package/tar"
    require "fpm/package/cpan"
    require "fpm/package/pear"
    require "fpm/package/empty"
    require "fpm/package/puppet"
    require "fpm/package/python"
    require "fpm/package/osxpkg"
    require "fpm/package/solaris"
    require "fpm/package/p5p"
    require "fpm/package/pkgin"
    require "fpm/package/freebsd"
    require "fpm/package/apk"

As you can see, there are several require statements for the
subdirectory at fpm/package/.

I think this is a very common use case. I encounter it myself
a lot in (almost) daily writing of ruby code, where I have to
load code stored in .rb files spread out.

Of course there are workarounds over the above, e. g. the
Dir[] or Dir.glob example that was given here (and the former
I use a lot). But I think it may be nicer to have an official
API support this as well.

The name could be:

    require_files

The first argument could be the path to the subdirectory at
hand; the second argument could be an options Hash that allows
more fine-tuning, such as traversing subdirectories, handling
.so files as well, or exclusively, and so on and so forth.

I believe it may fit into the "require" family, since that 
already has e. g. require_relative.

In the long run it would be nice to even be able to refer to
.rb files without having to use any hardcoded path at all -
but for the time being, any support for requiring/loading
files helps a lot.

(To the issue of dependencies in said .rb files, I usually
batch-load the .rb files, and if I get some error about
an uninitialized constant, I add it into that .rb file at
hand. It's a bit cumbersome but I understand that this part
is not easy to change presently.)

I think require_directory() is a better name that require_tree()
but I also like require_files().

The more important part is to want to convince that this is 
a common pattern, which is also why I added an example from
a quite popular ruby project (fpm presently has ~7.3 million
downloads on rubygems.org).

What I encounter myself doing is that, for my larger projects
in ruby, I end up creating a subdirectory called requires/ and
in that directory I put .rb files that handle loading of 
require-related activities, including subdirectories and external
dependencies.

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

* 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>