https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112015-12-09T16:17:23ZRuby Issue Tracking SystemRuby master - Feature #11797: `Enumerator#with_object` with multiple objectshttps://bugs.ruby-lang.org/issues/11797?journal_id=554112015-12-09T16:17:23Zsawa (Tsuyoshi Sawada)
<ul></ul><p>The StackOverflow question link was wrong. It should be: <a href="http://stackoverflow.com/questions/34181231" class="external">http://stackoverflow.com/questions/34181231</a>.</p> Ruby master - Feature #11797: `Enumerator#with_object` with multiple objectshttps://bugs.ruby-lang.org/issues/11797?journal_id=556472015-12-18T04:38:30Zshugo (Shugo Maeda)
<ul></ul><p>Hello,</p>
<p>I've removed his address from ruby-core and ruby-talk.</p>
<p>2015-12-18 13:27 GMT+09:00 Joseph Jones <a href="mailto:joeyi5216@gmail.com" class="email">joeyi5216@gmail.com</a>:</p>
<blockquote>
<p>Joseph Jones liked your message with Boxer.</p>
<p>On December 9, 2015 at 09:15:54 MST, <a href="mailto:sawadatsuyoshi@gmail.com" class="email">sawadatsuyoshi@gmail.com</a> wrote:</p>
<p>Issue <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: `Enumerator#with_object` with multiple objects (Closed)" href="https://bugs.ruby-lang.org/issues/11797">#11797</a> has been reported by Tsuyoshi Sawada.</p>
<hr>
<p>Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: `Enumerator#with_object` with multiple objects (Closed)" href="https://bugs.ruby-lang.org/issues/11797">#11797</a>: <code>Enumerator#with_object</code> with multiple objects<br>
<a href="https://bugs.ruby-lang.org/issues/11797" class="external">https://bugs.ruby-lang.org/issues/11797</a></p>
<ul>
<li>Author: Tsuyoshi Sawada</li>
<li>Status: Open</li>
<li>Priority: Normal</li>
<li>Assignee:</li>
</ul>
<hr>
<p>Sometimes, when working with <code>Enumerator#with_object</code>, I want to keep some<br>
additional temporary objects besides the one to return. A use case is as<br>
follows (I got this from this StackOverflow question:<br>
<a href="http://stackoverflow.com/questions/3418123" class="external">http://stackoverflow.com/questions/3418123</a>). Suppose I have an enumerator<br>
created from an array:</p>
<p>e = ["a", "b", "c", "c", "a", "c"].to_enum</p>
<p>and want to get an array of its repeated elements in the order they are<br>
repeated (i.e., appears for the second time):</p>
<a name="gt-c-a"></a>
<h1 >=> ["c", "a"]<a href="#gt-c-a" class="wiki-anchor">¶</a></h1>
<p>I can do it using <code>Enumerator#with_object</code> like this:</p>
<p>e.to_enum.with_object([{}, []]){|c, (h, a)| h[c] ? a.push(c) : h.store(c,<br>
true)}.last.uniq</p>
<p>Here, I am getting the array referred to as <code>a</code> in the block, but besides<br>
that, I am using a temporal hash <code>h</code>. I thought it would be nice if<br>
<code>Enumerator#with_object</code> accepts one or more objects, pass them individually<br>
as block arguments, and returns only the last argument so that I can do<br>
this:</p>
<p>e.to_enum.with_object({}, []){|c, h, a| h[c] ? a.push(c) : h.store(c,<br>
true)}.uniq</p>
<p>--<br>
<a href="https://bugs.ruby-lang.org/" class="external">https://bugs.ruby-lang.org/</a></p>
</blockquote>
<p>--<br>
Shugo Maeda</p> Ruby master - Feature #11797: `Enumerator#with_object` with multiple objectshttps://bugs.ruby-lang.org/issues/11797?journal_id=779642019-05-09T06:21:29Zsawa (Tsuyoshi Sawada)
<ul></ul><p>I now realize that, for the given use case, I can use <code>with_object</code> multiple times as follows:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">e</span>
<span class="p">.</span><span class="nf">each</span><span class="p">.</span><span class="nf">with_object</span><span class="p">({}).</span><span class="nf">with_object</span><span class="p">([])</span> <span class="p">{</span><span class="o">|</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">h</span><span class="p">),</span> <span class="n">a</span><span class="o">|</span> <span class="n">h</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="p">?</span> <span class="n">a</span><span class="p">.</span><span class="nf">push</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="p">:</span> <span class="n">h</span><span class="p">.</span><span class="nf">store</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="kp">true</span><span class="p">)}</span>
<span class="p">.</span><span class="nf">uniq</span>
</code></pre>
<p>So I withdraw this feature request. Please close it.</p> Ruby master - Feature #11797: `Enumerator#with_object` with multiple objectshttps://bugs.ruby-lang.org/issues/11797?journal_id=779672019-05-09T06:49:07Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul>