Will ruby support require 'path/to/dir/**' in the future

for instance, there are two exist files, 'path/to/dir/file' and
'path/to/dir/file2'
require 'path/to/dir/**'

is eql with

require 'path/to/dir/file'
require 'path/to/dir/file2'

The idiomatic way to do this in ruby is to make a "manifest" file outside of "/dir/.." called "dir.rb" that handles requiring everything inside it. This is commonly done in rubygems, e.g.

     require 'active_support'

     # active_support.rb
     require 'active_support/time'
     require 'active_support/...'

Then you can require all of it or just the components you need.

I don't see the need for "**". Also, with files coming from multiple locations in the load path, wildcards just seem like a bad idea.

Andrew Vit

···

On 14-10-03, 8:54, roro codeath wrote:

for instance, there are two exist files, 'path/to/dir/file' and
'path/to/dir/file2'
require 'path/to/dir/**'

is eql with

require 'path/to/dir/file'
require 'path/to/dir/file2'

It’s not for security reasons. If someone can write arbitrary files into a directory you’re requiring wholesale, they can probably delete and write new files too.

I’d consider it to not exist just because you rarely need it. When I have a directory full of files, I’m probably only requiring one or two at a time, and letting those files sort out their requirements themselves. In other cases, I just have the parent file outside the directory load them all in a correct order; i.e. the base class before any subclasses, like this:

%w{ operation base inner_register inner_flag counter inner_counter etc }.each do |f|
  require "riak/crdt/#{f}"
end

···

On Oct 3, 2014, at 12:49, Ammar Ali <ammarabuali@gmail.com> wrote:

On Oct 3, 2014, at 6:54 PM, roro codeath <rorocodeath@gmail.com> wrote:

for instance, there are two exist files, 'path/to/dir/file' and 'path/to/dir/file2'
require 'path/to/dir/**’

Most probably, no. For security reasons.

Hi roro,
roro codeath <rorocodeath@gmail.com> writes:

for instance, there are two exist files, 'path/to/dir/file' and
'path/to/dir/file2'
require 'path/to/dir/**'

is eql with

require 'path/to/dir/file'
require 'path/to/dir/file2'

You can achieve this with the “require_all” gem:

* require_all | RubyGems.org | your community gem host
* GitHub - jarmo/require_all: A wonderfully simple way to load Ruby code

Vale,
Quintus

···

--
Blog: http://www.quintilianus.eu

I will reject HTML emails. | Ich akzeptiere keine HTML-Nachrichten.
                               >
Use GnuPG for mail encryption: | GnuPG für Mail-Verschlüsselung:
http://www.gnupg.org | The GNU Privacy Guard

for instance, there are two exist files, 'path/to/dir/file' and 'path/to/dir/file2'
require 'path/to/dir/**’

Most probably, no. For security reasons.

is eql with

require 'path/to/dir/file'
require 'path/to/dir/file2’

That is not what it equals. It could equal:

require ‘path/to/dir/file’
require ‘path/to/dir/file2’
require ‘path/to/dir/some_thing_nasty’
require ‘path/to/dir/something_you_dont_want’
require ‘path/to/dir/god_knows_what’
require ‘path/to/dir/omg_what_is_this’
etc.

If *you* know (ruby doesn't) that you want all the files in a given directory, you can use Dir, something like:

Dir[‘path/to/dir/*.rb'].each {|file| require file }

But, I think that has the same security issue. A better way is to list the exact files you want to require, or if you don’t want to do that, maybe something like:

%w{file file2 file3}.each {|file|
  require “path/to/dir/#{file}"
}

Regards,
Ammar

···

On Oct 3, 2014, at 6:54 PM, roro codeath <rorocodeath@gmail.com> wrote:

for instance, there are two exist files, 'path/to/dir/file' and 'path/to/dir/file2'
require 'path/to/dir/**’

Most probably, no. For security reasons.

It’s not for security reasons. If someone can write arbitrary files into a directory you’re requiring wholesale, they can probably delete and write new files too.

I consider it a security risk, and if it existed, I would consider it poor practice. Require what you know you want.

%w{ operation base inner_register inner_flag counter inner_counter etc }.each do |f|
require "riak/crdt/#{f}"
end

That’s exactly the example I gave.

Regards,
Ammar

···

On Oct 3, 2014, at 7:56 PM, Bryce Kerley <bkerley@brycekerley.net> wrote:

On Oct 3, 2014, at 12:49, Ammar Ali <ammarabuali@gmail.com> wrote:
On Oct 3, 2014, at 6:54 PM, roro codeath <rorocodeath@gmail.com> wrote:

The risk of requiring a file you did not intend to require. I believe that reason was part of the rationale behind introducing require_relative in 1.9.

I could be paranoid about the security aspect, but that’s what most of security is about, not doing things that could be abused.

Even if the security concerns were negligible, or even unfounded, I still think it is poor practice to require all files from a directory en masse. It really doesn’t take that much effort to explicitly list them.

Regards,
Ammar

···

On Oct 3, 2014, at 7:14 PM, Arup Rakshit <aruprakshit@rocketmail.com> wrote:

On Friday, October 03, 2014 07:49:42 PM Ammar Ali wrote:

On Oct 3, 2014, at 6:54 PM, roro codeath <rorocodeath@gmail.com> wrote:

for instance, there are two exist files, 'path/to/dir/file' and
'path/to/dir/file2' require 'path/to/dir/**’

Most probably, no. For security reasons.

Could you tell what security reasons you are talking about ? Asking out of
curiosity.

Could you tell what security reasons you are talking about ? Asking out of
curiosity.

···

On Friday, October 03, 2014 07:49:42 PM Ammar Ali wrote:

On Oct 3, 2014, at 6:54 PM, roro codeath <rorocodeath@gmail.com> wrote:
> for instance, there are two exist files, 'path/to/dir/file' and
> 'path/to/dir/file2' require 'path/to/dir/**’

Most probably, no. For security reasons.

--

Regards,
Arup Rakshit

Debugging is twice as hard as writing the code in the first place. Therefore,
if you write the code as cleverly as possible, you are, by definition, not
smart enough to debug it.

--Brian Kernighan

i use rubymine, i hope ruby core team provide native support and rubymine
will inspect when index file system,ca you where to send this suggetion to
ruby-core team.

···

On Oct 4, 2014 12:38 AM, "Quintus" <quintus@quintilianus.eu> wrote:

Hi roro,
roro codeath <rorocodeath@gmail.com> writes:

> for instance, there are two exist files, 'path/to/dir/file' and
> 'path/to/dir/file2'
> require 'path/to/dir/**'
>
> is eql with
>
> require 'path/to/dir/file'
> require 'path/to/dir/file2'

You can achieve this with the “require_all” gem:

* require_all | RubyGems.org | your community gem host
* GitHub - jarmo/require_all: A wonderfully simple way to load Ruby code

Vale,
Quintus

--
Blog: http://www.quintilianus.eu

I will reject HTML emails. | Ich akzeptiere keine HTML-Nachrichten.
                               >
Use GnuPG for mail encryption: | GnuPG für Mail-Verschlüsselung:
http://www.gnupg.org | The GNU Privacy Guard

i know how to implement require_all, i only wanna add this feature when i
write simple cli. i think this feature is fit in following case

lib/my/core_ext/module.rb
lib/my/core_ext/class.rb
...

# lib/my/core_ext.rb
require 'lib/my/core_ext/**'

···

On Sat, Oct 4, 2014 at 1:08 AM, Ammar Ali <ammarabuali@gmail.com> wrote:

On Oct 3, 2014, at 7:56 PM, Bryce Kerley <bkerley@brycekerley.net> wrote:

>
>> On Oct 3, 2014, at 12:49, Ammar Ali <ammarabuali@gmail.com> wrote:
>>
>> On Oct 3, 2014, at 6:54 PM, roro codeath <rorocodeath@gmail.com> wrote:
>>
>>> for instance, there are two exist files, 'path/to/dir/file' and
'path/to/dir/file2'
>>> require 'path/to/dir/**’
>>
>> Most probably, no. For security reasons.
>
> It’s not for security reasons. If someone can write arbitrary files into
a directory you’re requiring wholesale, they can probably delete and write
new files too.

I consider it a security risk, and if it existed, I would consider it poor
practice. Require what you know you want.

> %w{ operation base inner_register inner_flag counter inner_counter etc
}.each do |f|
> require "riak/crdt/#{f}"
> end

That’s exactly the example I gave.

Regards,
Ammar

So... a vast minority of the time. I don't think this proposal has enough utility to outweigh the problems it causes.

···

On Oct 3, 2014, at 21:08, roro codeath <rorocodeath@gmail.com> wrote:

i know how to implement require_all, i only wanna add this feature when i write simple cli. [...]

I agree, it is a security risk and it should not be a standard
feature. Especially since it's so easy to hand code:

Dir["foo/**/*.rb"].each {|f| require f}

Oh, and btw. something these generic algorithms cannot do: they cannot
decide on the order to import which will work poorly when having
dependencies inside the library (which is usually the case) - you will
have to declare them properly in all the files anyway. And loading the
whole tree at once will also defy autoload. So, all in all, it's a bad
idea.

Kind regards

robert

···

On Sat, Oct 4, 2014 at 8:52 AM, Ryan Davis <ryand-ruby@zenspider.com> wrote:

On Oct 3, 2014, at 21:08, roro codeath <rorocodeath@gmail.com> wrote:

i know how to implement require_all, i only wanna add this feature when i write simple cli. [...]

So... a vast minority of the time. I don't think this proposal has enough utility to outweigh the problems it causes.

--
[guy, jim].each {|him| remember.him do |as, often| as.you_can - without end}
http://blog.rubybestpractices.com/