https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112020-05-06T14:30:20ZRuby Issue Tracking SystemRuby master - Feature #16833: Add Enumerable#empty?https://bugs.ruby-lang.org/issues/16833?journal_id=853902020-05-06T14:30:20Zf3ndot (Justin Bull)
<ul><li><strong>File</strong> <a href="/attachments/8399">add-enumerable-empty.patch</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/8399/add-enumerable-empty.patch">add-enumerable-empty.patch</a> added</li><li><strong>Description</strong> updated (<a title="View differences" href="/journals/85390/diff?detail_id=56971">diff</a>)</li></ul> Ruby master - Feature #16833: Add Enumerable#empty?https://bugs.ruby-lang.org/issues/16833?journal_id=853912020-05-06T16:34:12Zsawa (Tsuyoshi Sawada)
<ul></ul><p>Not sure what you mean by</p>
<blockquote>
<p>Enumerator, something mixed into Array</p>
</blockquote>
<p>Do you mean this?</p>
<blockquote>
<p>Enumerator, which is mixed into Array</p>
</blockquote>
<p>If that was what you meant, then that is not the case. <code>Array</code> mixes-in <code>Enumerable</code>, not <code>Enumerator</code>.</p>
<p>Even if that was the case, it is not clear how that is relevant to your argument. <code>Array</code> has its own <code>empty?</code> method, so even if its ancestor had an <code>empty?</code> method, the latter would be overridden by the former.</p> Ruby master - Feature #16833: Add Enumerable#empty?https://bugs.ruby-lang.org/issues/16833?journal_id=853922020-05-06T16:40:48Zsawa (Tsuyoshi Sawada)
<ul></ul><p>Your use case is not clear. Why can't you do this?</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">table_rows</span> <span class="o">=</span> <span class="p">[{</span> <span class="ss">data: </span><span class="p">[</span><span class="s1">'First'</span><span class="p">,</span> <span class="s1">'Second'</span><span class="p">,</span> <span class="s1">'Third'</span><span class="p">],</span> <span class="ss">config: </span><span class="p">{}</span> <span class="p">},</span> <span class="p">{</span> <span class="ss">data: </span><span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">],</span> <span class="ss">config: </span><span class="p">{</span> <span class="ss">color: </span><span class="s1">'red'</span> <span class="p">}</span> <span class="p">}].</span><span class="nf">to_enum</span>
<span class="n">table_rows</span><span class="p">.</span><span class="nf">to_a</span><span class="p">.</span><span class="nf">dig</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="ss">:data</span><span class="p">)</span> <span class="c1"># => ["First", "Second", "Third"]</span>
<span class="n">table_rows</span> <span class="o">=</span> <span class="p">[].</span><span class="nf">to_enum</span>
<span class="n">table_rows</span><span class="p">.</span><span class="nf">to_a</span><span class="p">.</span><span class="nf">dig</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="ss">:data</span><span class="p">)</span> <span class="c1"># => nil</span>
</code></pre>
<p>I don't understand why you need to conditionally return as in your code.</p> Ruby master - Feature #16833: Add Enumerable#empty?https://bugs.ruby-lang.org/issues/16833?journal_id=853932020-05-06T16:44:15Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>Enumerators can behave very differently than arrays. Some have side-effects. What's supposed to happen with those?</p>
<pre><code>require 'stringio'
s = StringIO.new("first\nsecond\nthird\nlast").each_line
s.first # => "first"
s.first # => "second"
s.empty? # => false
s.first # => "third" or "last"?
s.empty? # => false or true?
</code></pre>
<p>You may want to rethink your algorithm, or simply call <code>to_a</code> once and act on the array.</p>
<p>For similar reasons, <code>Enumerator#size</code> exist and is lazy, but it may return <code>nil</code> (e.g. <code>s.size</code> above) so wouldn't completely serve your purpose either.</p>
<p>For these reason I think you should either close your request or elaborate on an actual use case where a generic enumerator is being used, as well as the actual effect of <code>empty?</code> on external iterators, ...</p> Ruby master - Feature #16833: Add Enumerable#empty?https://bugs.ruby-lang.org/issues/16833?journal_id=853942020-05-06T19:55:43Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>sawa wrote:</p>
<blockquote>
<p>If that was what you meant, then that is not the case. Array mixes-in<br>
Enumerable, not Enumerator.</p>
</blockquote>
<p>Happens to me as well a lot - I always mix these two up. May be because<br>
the name is so similar. :)</p>
<p>To the suggestion itself: I have no particular preference either way so<br>
I won't really comment on the proposal itself.</p> Ruby master - Feature #16833: Add Enumerable#empty?https://bugs.ruby-lang.org/issues/16833?journal_id=1035772023-06-17T04:59:30Zioquatix (Samuel Williams)samuel@oriontransfer.net
<ul><li><strong>Assignee</strong> set to <i>ioquatix (Samuel Williams)</i></li></ul> Ruby master - Feature #16833: Add Enumerable#empty?https://bugs.ruby-lang.org/issues/16833?journal_id=1035782023-06-17T05:00:09Zioquatix (Samuel Williams)samuel@oriontransfer.net
<ul><li><strong>Subject</strong> changed from <i>Add Enumerator#empty?</i> to <i>Add Enumerable#empty?</i></li></ul>