https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112018-09-20T02:27:47ZRuby Issue Tracking SystemRuby master - Feature #15143: Extend `Enumerable#to_h`https://redmine.ruby-lang.org/issues/15143?journal_id=741062018-09-20T02:27:47Zsawa (Tsuyoshi Sawada)
<ul></ul><p>I forgot to mention that people alternatively do:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="mi">5</span><span class="p">).</span><span class="nf">each_with_object</span><span class="p">({}){</span><span class="o">|</span><span class="n">x</span><span class="p">,</span> <span class="n">h</span><span class="o">|</span> <span class="n">h</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="n">x</span> <span class="o">**</span> <span class="mi">2</span><span class="p">}</span>
<span class="c1">#=> {1=>1, 2=>4, 3=>9, 4=>16, 5=>25}</span>
</code></pre>
<p>which may be more optimal than calling <code>map</code>. The proposed feature also has the effect of making this technique unnecessary (although the proposal is not as effective as this in that the former creates intermediate subarrays).</p> Ruby master - Feature #15143: Extend `Enumerable#to_h`https://redmine.ruby-lang.org/issues/15143?journal_id=741072018-09-20T02:41:42Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul><li><strong>Assignee</strong> set to <i>nobu (Nobuyoshi Nakada)</i></li></ul><p>Sounds reasonable. Accepted.</p>
<p>Matz.</p> Ruby master - Feature #15143: Extend `Enumerable#to_h`https://redmine.ruby-lang.org/issues/15143?journal_id=741082018-09-20T02:47:00Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Is duplicate of</strong> <i><a class="issue tracker-2 status-5 priority-4 priority-default closed" href="/issues/10208">Feature #10208</a>: Passing block to Enumerable#to_h</i> added</li></ul> Ruby master - Feature #15143: Extend `Enumerable#to_h`https://redmine.ruby-lang.org/issues/15143?journal_id=741102018-09-20T02:47:54Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Subject</strong> changed from <i>Extend `Enumerrable#to_h`</i> to <i>Extend `Enumerable#to_h`</i></li></ul><p>Have you changed the mind since <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Passing block to Enumerable#to_h (Closed)" href="https://redmine.ruby-lang.org/issues/10208">#10208</a>?</p> Ruby master - Feature #15143: Extend `Enumerable#to_h`https://redmine.ruby-lang.org/issues/15143?journal_id=741112018-09-20T03:04:46Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>If the block returns an array whose length is > 2, how does it behave?</p>
<pre><code>%w(aa1 ab2 ba3 bb4).to_h {|s| s.chars }
#=> { "a" => { "a" => "1",
"b" => "2" },
"b" => { "a" => "3",
"b" => "4" } }
</code></pre>
<p>I expect very useful trie generator :-)</p> Ruby master - Feature #15143: Extend `Enumerable#to_h`https://redmine.ruby-lang.org/issues/15143?journal_id=741172018-09-20T06:25:25Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>nobu (Nobuyoshi Nakada) wrote:</p>
<blockquote>
<p>Have you changed the mind since <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Passing block to Enumerable#to_h (Closed)" href="https://redmine.ruby-lang.org/issues/10208">#10208</a>?</p>
</blockquote>
<p>Or since... <a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: Enumerable::to_hash (Rejected)" href="https://redmine.ruby-lang.org/issues/666">#666</a>!</p> Ruby master - Feature #15143: Extend `Enumerable#to_h`https://redmine.ruby-lang.org/issues/15143?journal_id=741182018-09-20T06:54:50Zsawa (Tsuyoshi Sawada)
<ul></ul><p>Sorry that I missed the previous duplicate proposals (I found that I had even commented on one of them, which I had forgotten about). But it looks like four years have passed since then, and the situation has changed perhaps. Or, it may have worth proposing again to indicate that many people want such feature.</p>
<p>I am grateful to Matz for accepting this.</p> Ruby master - Feature #15143: Extend `Enumerable#to_h`https://redmine.ruby-lang.org/issues/15143?journal_id=741192018-09-20T07:14:19Zsawa (Tsuyoshi Sawada)
<ul></ul><p>mame (Yusuke Endoh) wrote:</p>
<blockquote>
<p>If the block returns an array whose length is > 2, how does it behave?</p>
<pre><code>%w(aa1 ab2 ba3 bb4).to_h {|s| s.chars }
#=> { "a" => { "a" => "1",
"b" => "2" },
"b" => { "a" => "3",
"b" => "4" } }
</code></pre>
<p>I expect very useful trie generator :-)</p>
</blockquote>
<p>My original proposal is to follow the behaviour of the current <code>map.{...}.to_h</code>, which means to raise <code>ArgumentError: wrong array length at 0 (expected 2, was 3)</code>.</p>
<p>However, mame's proposal to extend the feature also sounds interesting and good. Though, after having Matz' acceptance of this proposal, I am not sure whether we can still get another approval on top of it from him assuming mame's extension, or whether the extension should be another issue.</p> Ruby master - Feature #15143: Extend `Enumerable#to_h`https://redmine.ruby-lang.org/issues/15143?journal_id=741202018-09-20T07:26:01Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>I changed my mind when I approved <code>to_h</code>.</p>
<p>Matz.</p> Ruby master - Feature #15143: Extend `Enumerable#to_h`https://redmine.ruby-lang.org/issues/15143?journal_id=741212018-09-20T07:27:57Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>I don't agree with @mame's idea. The return value from the block should be strictly 2 elements array.</p>
<p>Matz.</p> Ruby master - Feature #15143: Extend `Enumerable#to_h`https://redmine.ruby-lang.org/issues/15143?journal_id=741222018-09-20T07:32:44Zsawa (Tsuyoshi Sawada)
<ul></ul><p>Matz, thanks for confirmation. I am happy with my original proposal.</p> Ruby master - Feature #15143: Extend `Enumerable#to_h`https://redmine.ruby-lang.org/issues/15143?journal_id=741252018-09-20T11:48:54Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>OK, not only <code>Enumerable</code>, but <code>Array</code>, <code>Hash</code>, <code>ENV</code>, <code>NilClass</code> and <code>Struct</code> too?</p> Ruby master - Feature #15143: Extend `Enumerable#to_h`https://redmine.ruby-lang.org/issues/15143?journal_id=741262018-09-20T12:38:51Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p><code>nil</code> isn't an <code>Enumerable</code>, so it's irrelevant.</p> Ruby master - Feature #15143: Extend `Enumerable#to_h`https://redmine.ruby-lang.org/issues/15143?journal_id=741282018-09-20T15:06:59Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset trunk|r64794.</p>
<hr>
<p>Enumerable#to_h with block and so on</p>
<p>[Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Extend `Enumerable#to_h` (Closed)" href="https://redmine.ruby-lang.org/issues/15143">#15143</a>]</p> Ruby master - Feature #15143: Extend `Enumerable#to_h`https://redmine.ruby-lang.org/issues/15143?journal_id=741322018-09-21T02:23:46Zsawa (Tsuyoshi Sawada)
<ul></ul><p>nobu (Nobuyoshi Nakada) wrote:</p>
<blockquote>
<p>OK, not only <code>Enumerable</code>, but <code>Array</code>, <code>Hash</code>, <code>ENV</code>, <code>NilClass</code> and <code>Struct</code> too?</p>
</blockquote>
<p>Thanks for reminding me about that and implementing it.</p>