Feature #11302
closedDir.entries and Dir.foreach without [".", ".."]
Added by naruse (Yui NARUSE) over 10 years ago. Updated about 8 years ago.
Description
Dir.entries returns an array of its content with "." and "..".
But as far as I met, almost all cases don't need them.
How about adding such new method or options?
Updated by nobu (Nobuyoshi Nakada) over 10 years ago
Actions
#1
[ruby-dev:49136]
Candidates for the methods or options?
I prefer a same option for both methods, but no concrete idea.
Updated by red (Arnaud Rouyer) over 10 years ago
Actions
#2
[ruby-dev:49138]
Nobuyoshi Nakada wrote:
Candidates for the methods or options?
I prefer a same option for both methods, but no concrete idea.
Dir.foreach and Dir.entries both support a second hash argument for options: as of 2.2.2, the docs only mention the :encoding key in the options hash.
Basing myself on the GNU ls util, I propose supporting an :ignore key in the optional hash argument.
We could have an API similar to this:
$ ls -a
.
..
.hidden_file
directory
file.bin
$ irb
irb:001> Dir.entries('.')
=> [".", "..", ".hidden_file", "directory", "file.bin"]
irb:002> Dir.entries('.', ignore: :almost_all) # almost_all option name taken from GNU ls option name
=> [".hidden_file", "directory", "file.bin"] # http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/ls.c#n4784
irb:003> Dir.entries('.', ignore: :directories)
=> [".hidden_file", "file.bin"]
irb:004> Dir.entries('.', ignore: :hidden)
=> ["directory", "file.bin"]
# Fancy proposal
irb:005> Dir.entries('.', ignore: /o/)
=> [".", "..", ".hidden_file", "file.bin"]
Edit after a closer look:
Internally, Dir.entries and Dir.foreach both call Dir.new and use the resulting Dir object's #to_a and #each methods respectively to return an array/an enumerator. If we want to go down this path, these options have to be supported in Dir.new, stored in the Dir instance and reused in the #each enumerator to filter out ignored entries.
Updated by olivierlacan (Olivier Lacan) over 8 years ago
Actions
#3
[ruby-dev:50021]
red (Arnaud Rouyer) wrote:
Basing myself on the GNU ls util, I propose supporting an
:ignorekey in the optional hash argument.
I very much like this. I just ran into this issue myself today having to remove . and .. from Dir.entries output.
I don't think the ignore option accepting a regex is fancy at all, it makes a ton of sense. An array should also be acceptable considering that Dir.entries('.', ignore: %w[. ..]) would become equivalent to:
Dir.entries('.') - %w[. ..]
I find it quite elegant, and certainly a lot more discoverable than GNU ls style arguments. :-)
Updated by nobu (Nobuyoshi Nakada) over 8 years ago
Actions
#4
[ruby-dev:50087]
red (Arnaud Rouyer) wrote:
Basing myself on the GNU ls util, I propose supporting an
:ignorekey in the optional hash argument.
irb:002> Dir.entries('.', ignore: :almost_all) # almost_all option name taken from GNU ls option name
ignore: :almost_all seems like that almost all files will be ignored and only '.' and '..' will be returned.
Updated by akr (Akira Tanaka) over 8 years ago
Actions
#5
[ruby-dev:50126]
There is Pathname#children and Pathname#each_child.
How about Dir.children and Dir.each_child ?
Updated by shyouhei (Shyouhei Urabe) over 8 years ago
Actions
#6
[ruby-dev:50127]
+1 for Dir.children
Updated by matz (Yukihiro Matsumoto) over 8 years ago
Actions
#7
[ruby-dev:50128]
Sounds good.
Matz.
Updated by nobu (Nobuyoshi Nakada) over 8 years ago
Actions
#8
- Status changed from Assigned to Closed
Applied in changeset trunk|r58879.
dir.c: Dir.each_child and Dir.children
- dir.c (dir_s_each_child, dir_s_children): Dir.each_child and
Dir.children which are similar to Dir.foreach and Dir.entries
respectively, except to exclude "." and "..". [Feature #11302]
Updated by Eregon (Benoit Daloze) about 8 years ago
Actions
#9
- Related to Feature #13789: Dir - methods added
Updated by Eregon (Benoit Daloze) about 8 years ago
Actions
#10
[ruby-dev:50242]
The decision here is surprising given https://bugs.ruby-lang.org/issues/13789#note-3
but nevertheless I'm very happy this got accepted.
Updated by znz (Kazuhiro NISHIYAMA) about 8 years ago
Actions
#11
- Related to Feature #13969: Dir#each_child added