https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112015-06-19T08:48:02ZRuby Issue Tracking SystemRuby master - Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates.https://bugs.ruby-lang.org/issues/11286?journal_id=530472015-06-19T08:48:02Zzenspider (Ryan Davis)
<ul></ul><p>I rather like this proposal. I hope it gets accepted.</p> Ruby master - Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates.https://bugs.ruby-lang.org/issues/11286?journal_id=530542015-06-19T14:22:18Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li></ul><p>I agree. Assigning to Matz</p> Ruby master - Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates.https://bugs.ruby-lang.org/issues/11286?journal_id=530612015-06-19T21:54:12Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><blockquote>
<pre><code class="diff syntaxhl" data-language="diff"><span class="gi">+ struct MEMO *memo = MEMO_NEW(Qtrue, *argv, 0);
+ rb_check_arity(argc, 0, 1);
</span></code></pre>
</blockquote>
<p>Why dereference <code>argv</code> before checking <code>argc</code>.</p> Ruby master - Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates.https://bugs.ruby-lang.org/issues/11286?journal_id=530622015-06-19T22:14:14Z0x0dea (D.E. Akers)0x0dea+redmine@gmail.com
<ul><li><strong>File</strong> <a href="/attachments/5325">case_equality_sequence_predicates-check_argc_before_deref.patch</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/5325/case_equality_sequence_predicates-check_argc_before_deref.patch">case_equality_sequence_predicates-check_argc_before_deref.patch</a> added</li></ul><p>Nobuyoshi Nakada wrote:</p>
<blockquote>
<blockquote>
<pre><code class="diff syntaxhl" data-language="diff"><span class="gi">+ struct MEMO *memo = MEMO_NEW(Qtrue, *argv, 0);
+ rb_check_arity(argc, 0, 1);
</span></code></pre>
</blockquote>
<p>Why dereference <code>argv</code> before checking <code>argc</code>.</p>
</blockquote>
<p>I assumed <code>*argv</code> would resolve to <code>Qnil</code> in the case of no arguments, but this is indeed not the case. I have attached a modified patch which takes this into account.</p> Ruby master - Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates.https://bugs.ruby-lang.org/issues/11286?journal_id=530632015-06-20T00:12:23Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>D.E. Akers wrote:</p>
<blockquote>
<p>I assumed <code>*argv</code> would resolve to <code>Qnil</code> in the case of no arguments, but this is indeed not the case.</p>
</blockquote>
<p>Nobody guarantees it.</p>
<p>And argument check should be before making a memo object, I think.</p>
<pre><code class="diff syntaxhl" data-language="diff"><span class="gh">diff --git a/enum.c b/enum.c
index c5b9d77..d6f11e5 100644
</span><span class="gd">--- a/enum.c
</span><span class="gi">+++ b/enum.c
</span><span class="p">@@ -1045,6 +1045,8 @@</span> enum_sort_by(VALUE obj)
#define ENUMFUNC(name, argc) argc ? name##_eqq : rb_block_given_p() ? name##_iter_i : name##_i
<span class="gi">+#define MEMO_ENUM_NEW(v1) (rb_check_arity(argc, 0, 1), MEMO_NEW((v1), (argc ? *argv : 0), 0))
+
</span> #define DEFINE_ENUMFUNCS(name) \
static VALUE enum_##name##_func(VALUE result, struct MEMO *memo); \
\
<span class="p">@@ -1104,8 +1106,7 @@</span> DEFINE_ENUMFUNCS(all)
static VALUE
enum_all(int argc, VALUE *argv, VALUE obj)
{
<span class="gd">- struct MEMO *memo = MEMO_NEW(Qtrue, argc ? *argv : 0, 0);
- rb_check_arity(argc, 0, 1);
</span><span class="gi">+ struct MEMO *memo = MEMO_ENUM_NEW(Qtrue);
</span> rb_block_call(obj, id_each, 0, 0, ENUMFUNC(all, argc), (VALUE)memo);
return memo->v1;
}
<span class="p">@@ -1145,8 +1146,7 @@</span> DEFINE_ENUMFUNCS(any)
static VALUE
enum_any(int argc, VALUE *argv, VALUE obj)
{
<span class="gd">- struct MEMO *memo = MEMO_NEW(Qfalse, argc ? *argv : 0, 0);
- rb_check_arity(argc, 0, 1);
</span><span class="gi">+ struct MEMO *memo = MEMO_ENUM_NEW(Qfalse);
</span> rb_block_call(obj, id_each, 0, 0, ENUMFUNC(any, argc), (VALUE)memo);
return memo->v1;
}
<span class="p">@@ -1438,8 +1438,7 @@</span> DEFINE_ENUMFUNCS(none)
static VALUE
enum_none(int argc, VALUE *argv, VALUE obj)
{
<span class="gd">- struct MEMO *memo = MEMO_NEW(Qtrue, argc ? *argv : 0, 0);
- rb_check_arity(argc, 0, 1);
</span><span class="gi">+ struct MEMO *memo = MEMO_ENUM_NEW(Qtrue);
</span> rb_block_call(obj, id_each, 0, 0, ENUMFUNC(none, argc), (VALUE)memo);
return memo->v1;
}
</code></pre> Ruby master - Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates.https://bugs.ruby-lang.org/issues/11286?journal_id=530642015-06-20T01:13:13Z0x0dea (D.E. Akers)0x0dea+redmine@gmail.com
<ul><li><strong>File</strong> <a href="/attachments/5326">case_equality_sequence_predicates-all_updates.patch</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/5326/case_equality_sequence_predicates-all_updates.patch">case_equality_sequence_predicates-all_updates.patch</a> added</li></ul><p>I knew the call to <code>rb_check_arity()</code> should come first, but your very nice solution to doing so without mixing declarations and code did not occur to me. The attached contains all of the improvements discussed so far, but maybe there are more to be found? Is there something cleaner than passing <code>argc</code> to <code>ENUMFUNC()</code>?</p> Ruby master - Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates.https://bugs.ruby-lang.org/issues/11286?journal_id=534392015-07-16T17:30:15Z0x0dea (D.E. Akers)0x0dea+redmine@gmail.com
<ul></ul><p>matz, would you mind voicing your opinion of this proposal?</p> Ruby master - Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates.https://bugs.ruby-lang.org/issues/11286?journal_id=547222015-11-05T21:53:45Zshan (Shannon Skipper)
<ul></ul><p>This seems quite nice. Any downsides?</p> Ruby master - Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates.https://bugs.ruby-lang.org/issues/11286?journal_id=547232015-11-05T22:09:07Z0x0dea (D.E. Akers)0x0dea+redmine@gmail.com
<ul></ul><p>I've come to realize that it might not be entirely clear what is being proposed. In essence, all of the following examples feel very "Ruby" and should, in my opinion, Just Work.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mf">3.14</span><span class="p">,</span> <span class="mi">2</span><span class="n">ri</span><span class="p">].</span><span class="nf">all?</span><span class="p">(</span><span class="no">Numeric</span><span class="p">)</span> <span class="c1"># => true</span>
<span class="k">if</span> <span class="n">should_be_all_symbols</span><span class="p">.</span><span class="nf">any?</span><span class="p">(</span><span class="no">String</span><span class="p">)</span>
<span class="o">...</span>
<span class="k">end</span>
<span class="n">some_strings</span><span class="p">.</span><span class="nf">none?</span><span class="p">(</span><span class="sr">/aeiou/i</span><span class="p">)</span>
<span class="n">lotto</span><span class="p">.</span><span class="nf">all?</span><span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="mi">49</span><span class="p">)</span> <span class="o">&&</span> <span class="n">lotto</span><span class="p">.</span><span class="nf">one?</span><span class="p">(</span><span class="mi">43</span><span class="o">..</span><span class="mi">49</span><span class="p">)</span>
</code></pre> Ruby master - Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates.https://bugs.ruby-lang.org/issues/11286?journal_id=607582016-10-06T00:22:08Zzenspider (Ryan Davis)
<ul></ul><p>Why is this still open a year later?</p>
<p>Matz? A ruling?</p> Ruby master - Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates.https://bugs.ruby-lang.org/issues/11286?journal_id=607702016-10-06T21:27:59Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>I like this one in particular:</p>
<pre><code>some_strings.none?(/aeiou/i)
</code></pre>
<p>I am not so sure about:</p>
<pre><code>nums.grep(5..10)
</code></pre>
<p>But I have no strong feelings either way. I do disagree with the<br>
statement "syntactic noise incurred by opening a block" though -<br>
blocks feel very natural in ruby to me. I guess the main part I<br>
would agree is that the .grep(range) variant is shorter to write<br>
than the block variant. This also reminds me of other proposals<br>
about allowing arguments to e. g. .map(&:chomp) invocations -<br>
I use this quite a lot myself but the & there still "feels" sort<br>
of strange.</p> Ruby master - Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates.https://bugs.ruby-lang.org/issues/11286?journal_id=621922016-12-21T14:56:52Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>We looked at this issue in today's developer meeting.</p>
<p>Attendees were positive about the proposed functionality. But Matz wanted separate method(s) than to extend all? etc, like we have separate select and grep methods.</p> Ruby master - Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates.https://bugs.ruby-lang.org/issues/11286?journal_id=621952016-12-21T16:03:50Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>I am positive about the idea too. But as a duty of the final decision maker, I have to consider every option before the judgment. I see a few additional options.</p>
<ul>
<li>takes optional argument that takes <code>===</code> (proposed)</li>
<li>takes optional keyword argument, <code>match:</code> for example</li>
<li>introduces another set of methods with different names</li>
</ul>
<p>Right now, I am wandering between option 1 and 2.</p>
<p>Matz.</p> Ruby master - Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates.https://bugs.ruby-lang.org/issues/11286?journal_id=622302016-12-24T17:08:39Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-5 priority-4 priority-default closed" href="/issues/13067">Feature #13067</a>: TrueClass,FalseClass to provide `===` to match truthy/falsy values.</i> added</li></ul> Ruby master - Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates.https://bugs.ruby-lang.org/issues/11286?journal_id=679542017-11-28T04:26:43Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>matz (Yukihiro Matsumoto) wrote:</p>
<blockquote>
<p>I am positive about the idea too. But as a duty of the final decision maker, I have to consider every option before the judgment. I see a few additional options.</p>
<ul>
<li>takes optional argument that takes <code>===</code> (proposed)</li>
<li>takes optional keyword argument, <code>match:</code> for example</li>
<li>introduces another set of methods with different names</li>
</ul>
<p>Right now, I am wandering between option 1 and 2.</p>
</blockquote>
<p>I hope a decision is made on this.</p>
<p>While I love keyword arguments in general, I am not convinced they are needed here and I would favor solution #1.</p> Ruby master - Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates.https://bugs.ruby-lang.org/issues/11286?journal_id=680372017-11-29T09:37:00Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>OK, I made a decision. I took option 1 above as proposed.<br>
We can do as following:</p>
<pre><code>[1, 3.14, 2ri].all?(Numeric) # => true
if should_be_all_symbols.any?(String)
...
end
some_strings.none?(/aeiou/i)
</code></pre>
<p>Matz.</p> Ruby master - Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates.https://bugs.ruby-lang.org/issues/11286?journal_id=682542017-12-10T22:41:16Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li><li><strong>Target version</strong> set to <i>2.5</i></li></ul><p>Thank you Matz</p>
<p>D.E. Akers: thanks for the patch! I adapted it for better handling of enumerables yielding multiple arguments, and also changing Hash#any? / Array#any? which have their own versions.</p>
<p>Merged.</p> Ruby master - Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates.https://bugs.ruby-lang.org/issues/11286?journal_id=685002017-12-18T14:12:03Zznz (Kazuhiro NISHIYAMA)
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-1 priority-4 priority-default" href="/issues/14197">Feature #14197</a>: `Enumerable#{select,reject}` accept a pattern argument</i> added</li></ul>