There must be a better way -- requiring multiple files

Could Kernel#require be modified to understand '*' (non-recursive) and
'**' (recursive)? Then you could do:

require "net/*" # require all .rb files under the net directory
require "net/**" # Same, but recursive

I seem to recall this being proposed in the past, but I can't remember
the arguments against it.

Regards,

Dan

···

-----Original Message-----
From: Robert Klemme [mailto:bob.news@gmx.net]
Sent: Tuesday, November 22, 2005 9:57 AM
To: ruby-talk ML
Subject: Re: There must be a better way -- requiring multiple files

Stefan Lang wrote:
> On Tuesday 22 November 2005 16:37, Robert Klemme wrote:
> [...]
>> Dir[File.join(__FILE__, "..", "..", "lib", "**",
"*.rb")].each {|l|
>> require l}
> [..]
>
> A side note:
>
> I have come to the conclusion that the following syntax makes more
> sense for Dir/Dir.glob:
>
> Dir["#{__FILE__}/../../lib/**/*.rb"]
>
> (In other words, File.join doesn't make sense here.)
>
> Why?
>
> 1) In my opinion, it is more readable

Yeah, probably.

> 2) After reading dir.c in Ruby's sources, I would say
> that it is at least as portable as the File.join variant.
> Others more familiar with the Ruby source already said
> that Ruby uses slashes internally on all platforms.

I'm a bit wary to rely on such knowledge about internals. I
prefer to stick with the interfaces.

But then again it might be a bit too much formalism in this
case - and especially for one shot scripts.

> 3) It's more efficient.

Yep, guess so.

Interesting points raised nevertheless.

Kind regards

    robert

You could add to them that it'd pose some problems with RubyGems' new
definition of Kernel#require.

···

On Wed, Nov 23, 2005 at 02:29:16AM +0900, Berger, Daniel wrote:

Could Kernel#require be modified to understand '*' (non-recursive) and
'**' (recursive)? Then you could do:

require "net/*" # require all .rb files under the net directory
require "net/**" # Same, but recursive

I seem to recall this being proposed in the past, but I can't remember
the arguments against it.

--
Mauricio Fernandez

Yeah, but I think that there are enough potential problems with the
idea that one wouldn't even have to reach that far down the list to
kill it.

1. The search time would be high, since Ruby would have to look in all
of $LOAD_PATH.
2. It would be *massively* insecure. Even what I do in Ruwiki isn't
safe from that perspective.

It would be better to have a single file that you load that contains
the names of the files you want to load.

-austin

···

On 11/22/05, Mauricio Fernández <mfp@acm.org> wrote:

On Wed, Nov 23, 2005 at 02:29:16AM +0900, Berger, Daniel wrote:
> Could Kernel#require be modified to understand '*' (non-recursive) and
> '**' (recursive)? Then you could do:
>
> require "net/*" # require all .rb files under the net directory
> require "net/**" # Same, but recursive
>
> I seem to recall this being proposed in the past, but I can't remember
> the arguments against it.
You could add to them that it'd pose some problems with RubyGems' new
definition of Kernel#require.

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca