https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112017-12-18T14:12:03ZRuby Issue Tracking SystemRuby master - Feature #14197: `Enumerable#{select,reject}` accept a pattern argumenthttps://bugs.ruby-lang.org/issues/14197?journal_id=684992017-12-18T14:12:03Zznz (Kazuhiro NISHIYAMA)
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-5 priority-4 priority-default closed" href="/issues/11286">Feature #11286</a>: [PATCH] Add case equality arity to Enumerable's sequence predicates.</i> added</li></ul> Ruby master - Feature #14197: `Enumerable#{select,reject}` accept a pattern argumenthttps://bugs.ruby-lang.org/issues/14197?journal_id=685682017-12-20T21:29:42Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>I think this suggestion looks ok, unless I may have missed something.</p>
<p>The main difference Kazuhiro is suggesting, appears to be the shorter<br>
notation via regex given to .reject() rather than use the (longer)<br>
block variant.</p>
<p>Here is the link to #grep:</p>
<p><a href="https://ruby-doc.org/core-2.1.0/Enumerable.html#method-i-grep" class="external">https://ruby-doc.org/core-2.1.0/Enumerable.html#method-i-grep</a></p>
<p>which shows one example:</p>
<pre><code>c = IO.constants
c.grep(/SEEK/) #=> [:SEEK_SET, :SEEK_CUR, :SEEK_END]
</code></pre>
<p>On a side note, while I have used .grep(), I have never used .grep_v()<br>
and it is a bit mysterious to me what grep_v does from the name<br>
alone. :)</p> Ruby master - Feature #14197: `Enumerable#{select,reject}` accept a pattern argumenthttps://bugs.ruby-lang.org/issues/14197?journal_id=689382017-12-25T18:15:14Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Target version</strong> deleted (<del><i>2.6</i></del>)</li></ul> Ruby master - Feature #14197: `Enumerable#{select,reject}` accept a pattern argumenthttps://bugs.ruby-lang.org/issues/14197?journal_id=695532018-01-11T21:32:47Zinopinatus (Joshua GOODALL)
<ul></ul><p>I have personally found it useful to implement Regexp#to_proc as in <a href="https://bugs.ruby-lang.org/issues/7883" class="external">https://bugs.ruby-lang.org/issues/7883</a> which permits collection.reject(&/re/) and more besides.</p> Ruby master - Feature #14197: `Enumerable#{select,reject}` accept a pattern argumenthttps://bugs.ruby-lang.org/issues/14197?journal_id=711482018-03-21T15:49:10Zxavriley (Xavier Riley)
<ul></ul><p>This issue is also related <a href="https://bugs.ruby-lang.org/issues/9602" class="external">https://bugs.ruby-lang.org/issues/9602</a></p>
<p>One interesting point there is the case where a pattern and a block are given:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="sx">%w{ant bear cat}</span><span class="p">.</span><span class="nf">select</span><span class="p">(</span><span class="sr">/bear/</span><span class="p">)</span> <span class="p">{</span><span class="o">||</span><span class="n">x</span><span class="o">|</span> <span class="n">x</span> <span class="o">===</span> <span class="s2">"cat"</span> <span class="p">}</span>
</code></pre>
<p>This issue also applies to the implementation from the linked issue for <code>#all?(pattern)</code>but this is already implemented</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="o">>></span> <span class="sx">%w{ant bear cat}</span><span class="p">.</span><span class="nf">all?</span><span class="p">(</span><span class="no">String</span><span class="p">)</span> <span class="p">{</span><span class="o">|</span><span class="n">x</span><span class="o">|</span> <span class="n">x</span> <span class="o">===</span> <span class="no">Integer</span> <span class="p">}</span>
<span class="o">=></span> <span class="kp">true</span>
</code></pre> Ruby master - Feature #14197: `Enumerable#{select,reject}` accept a pattern argumenthttps://bugs.ruby-lang.org/issues/14197?journal_id=711502018-03-21T16:10:36Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>The point is that the phrase "hard to remember" in the OP is bit weak when we already have methods with the proposed behavior (<code>grep</code> and <code>grep_v</code>).</p>
<p>Matz.</p> Ruby master - Feature #14197: `Enumerable#{select,reject}` accept a pattern argumenthttps://bugs.ruby-lang.org/issues/14197?journal_id=725212018-06-19T10:56:45Zbyroot (Jean Boussier)byroot@ruby-lang.org
<ul></ul><blockquote>
<p>The point is that the phrase "hard to remember" in the OP is bit weak when we already have methods with the proposed behavior (<code>grep</code> and <code>grep_v</code>).</p>
</blockquote>
<p>Indeed. However I think there is a case to be made for consistency.</p>
<p>If this works:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="sx">%(foo bar)</span><span class="p">.</span><span class="nf">all?</span><span class="p">(</span><span class="no">String</span><span class="p">)</span>
</code></pre>
<p>I do expect this to work as well.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="sx">%(foo bar)</span><span class="p">.</span><span class="nf">reject</span><span class="p">(</span><span class="no">String</span><span class="p">)</span>
</code></pre>
<p>It is true that then <code>select</code> would end up being an alias of <code>grep</code> and <code>reject</code> an alias of <code>grep_v</code>, but I actually see it as a good thing.</p> Ruby master - Feature #14197: `Enumerable#{select,reject}` accept a pattern argumenthttps://bugs.ruby-lang.org/issues/14197?journal_id=849192020-04-04T05:47:39Zsawa (Tsuyoshi Sawada)
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/84919/diff?detail_id=56778">diff</a>)</li></ul>