https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112015-06-24T06:41:19ZRuby Issue Tracking SystemRuby master - Feature #11302: Dir.entries and Dir.foreach without [".", ".."]https://redmine.ruby-lang.org/issues/11302?journal_id=531042015-06-24T06:41:19Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>Candidates for the methods or options?</p>
<p>I prefer a same option for both methods, but no concrete idea.</p> Ruby master - Feature #11302: Dir.entries and Dir.foreach without [".", ".."]https://redmine.ruby-lang.org/issues/11302?journal_id=531132015-06-24T15:44:12Zred (Arnaud Rouyer)josh.guthrie@gmail.com
<ul></ul><p>Nobuyoshi Nakada wrote:</p>
<blockquote>
<p>Candidates for the methods or options?</p>
<p>I prefer a same option for both methods, but no concrete idea.</p>
</blockquote>
<p><code>Dir.foreach</code> and <code>Dir.entries</code> both support a second hash argument for options: as of 2.2.2, the docs only mention the <code>:encoding</code> key in the options hash.</p>
<p>Basing myself on the GNU ls util, I propose supporting an <code>:ignore</code> key in the optional hash argument.</p>
<p>We could have an API similar to this:</p>
<pre><code>$ 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"]
</code></pre>
<p>Edit after a closer look:</p>
<p>Internally, <code>Dir.entries</code> and <code>Dir.foreach</code> both call <code>Dir.new</code> and use the resulting <code>Dir</code> object's <code>#to_a</code> and <code>#each</code> methods respectively to return an array/an enumerator. If we want to go down this path, these options have to be supported in <code>Dir.new</code>, stored in the <code>Dir</code> instance and reused in the <code>#each</code> enumerator to filter out ignored entries.</p> Ruby master - Feature #11302: Dir.entries and Dir.foreach without [".", ".."]https://redmine.ruby-lang.org/issues/11302?journal_id=636662017-03-19T05:10:56Zolivierlacan (Olivier Lacan)hi@olivierlacan.com
<ul></ul><p>red (Arnaud Rouyer) wrote:</p>
<blockquote>
<p>Basing myself on the GNU ls util, I propose supporting an <code>:ignore</code> key in the optional hash argument.</p>
</blockquote>
<p>I very much like this. I just ran into this issue myself today having to remove <code>.</code> and <code>..</code> from <code>Dir.entries</code> output.</p>
<p>I don't think the <code>ignore</code> option accepting a regex is fancy at all, it makes a ton of sense. An array should also be acceptable considering that <code>Dir.entries('.', ignore: %w[. ..])</code> would become equivalent to:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Dir</span><span class="p">.</span><span class="nf">entries</span><span class="p">(</span><span class="s1">'.'</span><span class="p">)</span> <span class="o">-</span> <span class="sx">%w[. ..]</span>
</code></pre>
<p>I find it quite elegant, and certainly a lot more discoverable than GNU ls style arguments. :-)</p> Ruby master - Feature #11302: Dir.entries and Dir.foreach without [".", ".."]https://redmine.ruby-lang.org/issues/11302?journal_id=643802017-04-19T02:06:10Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>red (Arnaud Rouyer) wrote:</p>
<blockquote>
<p>Basing myself on the GNU ls util, I propose supporting an <code>:ignore</code> key in the optional hash argument.</p>
</blockquote>
<blockquote>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">irb</span><span class="p">:</span><span class="mo">002</span><span class="o">></span> <span class="no">Dir</span><span class="p">.</span><span class="nf">entries</span><span class="p">(</span><span class="s1">'.'</span><span class="p">,</span> <span class="ss">ignore: :almost_all</span><span class="p">)</span> <span class="c1"># almost_all option name taken from GNU ls option name</span>
</code></pre>
</blockquote>
<p><code>ignore: :almost_all</code> seems like that almost all files will be ignored and only '.' and '..' will be returned.</p> Ruby master - Feature #11302: Dir.entries and Dir.foreach without [".", ".."]https://redmine.ruby-lang.org/issues/11302?journal_id=649362017-05-19T09:01:18Zakr (Akira Tanaka)akr@fsij.org
<ul></ul><p>There is <code>Pathname#children</code> and <code>Pathname#each_child</code>.</p>
<p>How about <code>Dir.children</code> and <code>Dir.each_child</code> ?</p> Ruby master - Feature #11302: Dir.entries and Dir.foreach without [".", ".."]https://redmine.ruby-lang.org/issues/11302?journal_id=649372017-05-19T09:06:16Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>+1 for Dir.children</p> Ruby master - Feature #11302: Dir.entries and Dir.foreach without [".", ".."]https://redmine.ruby-lang.org/issues/11302?journal_id=649382017-05-19T09:08:31Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>Sounds good.</p>
<p>Matz.</p> Ruby master - Feature #11302: Dir.entries and Dir.foreach without [".", ".."]https://redmine.ruby-lang.org/issues/11302?journal_id=650802017-05-25T02:50:26Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Closed</i></li></ul><p>Applied in changeset trunk|r58879.</p>
<hr>
<p>dir.c: Dir.each_child and Dir.children</p>
<ul>
<li>dir.c (dir_s_each_child, dir_s_children): Dir.each_child and<br>
Dir.children which are similar to Dir.foreach and Dir.entries<br>
respectively, except to exclude "." and "..". [Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Dir.entries and Dir.foreach without [".", ".."] (Closed)" href="https://redmine.ruby-lang.org/issues/11302">#11302</a>]</li>
</ul> Ruby master - Feature #11302: Dir.entries and Dir.foreach without [".", ".."]https://redmine.ruby-lang.org/issues/11302?journal_id=666522017-09-14T09:05:25ZEregon (Benoit Daloze)
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-6 priority-4 priority-default closed" href="/issues/13789">Feature #13789</a>: Dir - methods</i> added</li></ul> Ruby master - Feature #11302: Dir.entries and Dir.foreach without [".", ".."]https://redmine.ruby-lang.org/issues/11302?journal_id=666532017-09-14T09:07:24ZEregon (Benoit Daloze)
<ul></ul><p>The decision here is surprising given <a href="https://bugs.ruby-lang.org/issues/13789#note-3" class="external">https://bugs.ruby-lang.org/issues/13789#note-3</a><br>
but nevertheless I'm very happy this got accepted.</p> Ruby master - Feature #11302: Dir.entries and Dir.foreach without [".", ".."]https://redmine.ruby-lang.org/issues/11302?journal_id=670522017-10-04T11:11:56Zznz (Kazuhiro NISHIYAMA)
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-5 priority-4 priority-default closed" href="/issues/13969">Feature #13969</a>: Dir#each_child</i> added</li></ul>