https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112015-12-15T20:44:54ZRuby Issue Tracking SystemRuby master - Feature #11824: Add Enumerator#to_ary for added implicit behaviorhttps://redmine.ruby-lang.org/issues/11824?journal_id=555722015-12-15T20:44:54Zdanielpclark (Daniel P. Clark)6ftdan@gmail.com
<ul></ul><p>Perhaps I was too hasty posting this? According to Matz about 6 years ago this didn't seem the right thing to do.</p>
<blockquote>
<p>Hi,</p>
</blockquote>
<blockquote>
<p>In message "Re: [Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Recursive Enumerable#join is surprising (Closed)" href="https://redmine.ruby-lang.org/issues/1893">#1893</a>] Recursive Enumerable#join is surprising"<br>
on Wed, 3 Mar 2010 00:57:26 +0900, Yusuke Endoh <a href="mailto:redmine@ruby-lang.org" class="email">redmine@ruby-lang.org</a> writes:</p>
</blockquote>
<blockquote>
<p>|I guess adding Enumerator#to_ary is a right solution.</p>
</blockquote>
<blockquote>
<p>I don't think so, supplying to_ary means that object can be considered<br>
as an array, which is not always the case.</p>
</blockquote>
<blockquote>
<p>matz.</p>
</blockquote>
<p><a href="https://bugs.ruby-lang.org/issues/1893#note-7" class="external">https://bugs.ruby-lang.org/issues/1893#note-7</a></p>
<p>Maybe things have changed since Ruby 1.9.2 on this issue? I've been reading through the other issue thread and I'm not sure it would be a bad thing. Most of the methods from Array are already defined and it would save a lot of <code>to_a</code> calls from being written. It would make the following methods available on Enumerators.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Array</span><span class="p">.</span><span class="nf">instance_methods</span> <span class="o">-</span> <span class="no">Enumerator</span><span class="p">.</span><span class="nf">instance_methods</span>
<span class="c1"># => [:[], :[]=, :at, :fetch, :last, :concat, :<<, :push, :pop, :shift, :unshift, :insert, :each_index, :length, :empty?, :index, :rindex, :join, :reverse, :reverse!, :rotate, :rotate!, :sort!, :sort_by!, :collect!, :map!, :select!, :keep_if, :values_at, :delete, :delete_at, :delete_if, :reject!, :transpose, :replace, :clear, :fill, :slice, :slice!, :assoc, :rassoc, :+, :*, :-, :&, :|, :uniq, :uniq!, :compact, :compact!, :flatten, :flatten!, :shuffle!, :shuffle, :sample, :permutation, :combination, :repeated_permutation, :repeated_combination, :product, :bsearch, :pack]</span>
</code></pre> Ruby master - Feature #11824: Add Enumerator#to_ary for added implicit behaviorhttps://redmine.ruby-lang.org/issues/11824?journal_id=555732015-12-15T20:50:13Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li></ul><p>I'm pretty certain that it's still quite a bad idea.</p>
<p>Can you provide any argument why anything would have changed? Otherwise we'll close this.</p> Ruby master - Feature #11824: Add Enumerator#to_ary for added implicit behaviorhttps://redmine.ruby-lang.org/issues/11824?journal_id=555742015-12-15T21:48:48Zdanielpclark (Daniel P. Clark)6ftdan@gmail.com
<ul></ul><p>Other than syntactic cleanliness and possible public opinion, unfortunately no I don't have a compelling reason why things would have changed.</p>
<p>I think what I'd really like is to not have to call <code>to_a</code> on Enumerators for anything actionable (any verbs). I think you're right it may be a bad idea as many systems may have issue with this.</p>
<p>I suppose I could write my own custom class EnumArraytors ;-). It's probably best practice to create new classes rather than change how the language itself fundamentally works. Or I could use a Refinement to do this in my own projects whenever I want to clean the code base up.</p>
<p>Thanks for at least giving me an opportunity to defend the idea. It was worth considering the options available.</p>
<p>UPDATE: I just learned how to write my own Enumerator::Lazy implementation. This was actually what I was looking for to allow duplicate method implementation for Feature <a class="issue tracker-2 status-1 priority-4 priority-default" title="Feature: Proposal for method `Array#difference` (Open)" href="https://redmine.ruby-lang.org/issues/11815">#11815</a> . This solves the issue of why I opened this feature suggestion in the first place.</p>