https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?15675512322013-12-11T03:41:23ZRuby Issue Tracking SystemRuby master - Feature #9239: Array#to_h ignores flat arrayshttps://bugs.ruby-lang.org/issues/9239?journal_id=435882013-12-11T03:41:23Zmarcandre (Marc-Andre Lafortune)ruby-core@marc-andre.ca
<ul><li><strong>Tracker</strong> changed from <i>Bug</i> to <i>Feature</i></li></ul> Ruby master - Feature #9239: Array#to_h ignores flat arrayshttps://bugs.ruby-lang.org/issues/9239?journal_id=435892013-12-11T03:49:31Zmarcandre (Marc-Andre Lafortune)ruby-core@marc-andre.ca
<ul><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li></ul><p>Moving to &quot;feature&quot;, as the current documentation states that elements that are not 2-element arrays are ignored, so it is different from Hash.[]</p>
<p>My opinion is that the fact that <code>Hash[1,2,3,4] # =&gt; {1 =&gt; 2, 3 =&gt; 4}</code> is a misfeature and that this usage should not be encouraged.</p>
<p>On the other hand, Array#to_h could raise an error on elements that are not 2-element arrays. I argued otherwise before, but maybe that would be safer.</p>
Ruby master - Feature #9239: Array#to_h ignores flat arrayshttps://bugs.ruby-lang.org/issues/9239?journal_id=435902013-12-11T05:14:36Zalexeymuranov (Alexey Muranov)
<ul></ul><p>If [:a, :b].to_h returned {:a =&gt; :b}, then what should <a class="wiki-page new" href="https://bugs.ruby-lang.org/projects/ruby-trunk/wiki/A_b%5D_%5Bc_d">:a, :b], [:c, :d</a>.to_h return? {[:a, :b] =&gt; [:c, :d]} ?</p>
Ruby master - Feature #9239: Array#to_h ignores flat arrayshttps://bugs.ruby-lang.org/issues/9239?journal_id=435922013-12-11T11:22:54Zsawa (Tsuyoshi Sawada)
<ul></ul><p>marcandre (Marc-Andre Lafortune) wrote:</p>
<blockquote>
<p>My opinion is that the fact that <code>Hash[1,2,3,4] # =&gt; {1 =&gt; 2, 3 =&gt; 4}</code> is a misfeature and that this usage should not be encouraged.</p>
<p>On the other hand, Array#to_h could raise an error on elements that are not 2-element arrays. I argued otherwise before, but maybe that would be safer.</p>
</blockquote>
<p>I agree.</p>
Ruby master - Feature #9239: Array#to_h ignores flat arrayshttps://bugs.ruby-lang.org/issues/9239?journal_id=436812013-12-15T04:06:18Zmarcandre (Marc-Andre Lafortune)ruby-core@marc-andre.ca
<ul></ul><p>One reason I think I was wrong is that current form could encourage code like:</p>
<p>enum.map{|x| [x.foo, x.bar] if x.baz? }.to_h</p>
<p>using the fact that any <code>nil</code> will be ignored. I&#39;m not sure that it&#39;s a good idea.</p>
<p>It would probably be safer to raise an Exception for elements that are not a key-value pair. It also satisfies fail-early principle.</p>
<p>Matz, what do you think?</p>
Ruby master - Feature #9239: Array#to_h ignores flat arrayshttps://bugs.ruby-lang.org/issues/9239?journal_id=437262013-12-18T07:07:00Zmarcandre (Marc-Andre Lafortune)ruby-core@marc-andre.ca
<ul><li><strong>Category</strong> set to <i>core</i></li><li><strong>Assignee</strong> changed from <i>matz (Yukihiro Matsumoto)</i> to <i>marcandre (Marc-Andre Lafortune)</i></li><li><strong>Target version</strong> set to <i>2.1.0</i></li></ul><p>I got the green light from Matz, thanks :-)</p>
<p>Naruse-san: could you please confirm that there is no problem on your end for me to commit the following patch: <a href="https://github.com/marcandre/ruby/compare/to_h_raise">https://github.com/marcandre/ruby/compare/to_h_raise</a></p>
<p>Thanks</p>
Ruby master - Feature #9239: Array#to_h ignores flat arrayshttps://bugs.ruby-lang.org/issues/9239?journal_id=437292013-12-18T08:18:56Zzzak (Zachary Scott)zzak@ruby-lang.org
<ul></ul><p>I dont think we should commit this before 2.1, is there good reason?</p>
Ruby master - Feature #9239: Array#to_h ignores flat arrayshttps://bugs.ruby-lang.org/issues/9239?journal_id=437322013-12-18T12:45:31Zmarcandre (Marc-Andre Lafortune)ruby-core@marc-andre.ca
<ul></ul><p>Since this is a new feature, there&#39;s no risk of regression and there is no incompatibility. Committing after 2.1 could create incompatibility.</p>
Ruby master - Feature #9239: Array#to_h ignores flat arrayshttps://bugs.ruby-lang.org/issues/9239?journal_id=437362013-12-18T12:57:08Zzzak (Zachary Scott)zzak@ruby-lang.org
<ul></ul><p>How would committing after 2.1 create incompatibility?</p>
<p>If we commit this feature to trunk after ruby_2_1 branch is created, then it won&#39;t be released until 2.2, right?</p>
Ruby master - Feature #9239: Array#to_h ignores flat arrayshttps://bugs.ruby-lang.org/issues/9239?journal_id=437382013-12-18T13:21:55Zmarcandre (Marc-Andre Lafortune)ruby-core@marc-andre.ca
<ul></ul><p>zzak (Zachary Scott) wrote:</p>
<blockquote>
<p>How would committing after 2.1 create incompatibility?</p>
</blockquote>
<p>If some rubyists write code for Ruby 2.1 using this new Arry#to_h and rely on the fact that elements that are not key value pairs are ignored, then this change in 2.2 will break their code. This is what I mean by incompatibility. </p>
Ruby master - Feature #9239: Array#to_h ignores flat arrayshttps://bugs.ruby-lang.org/issues/9239?journal_id=437452013-12-18T22:54:30Zzzak (Zachary Scott)zzak@ruby-lang.org
<ul></ul><p>I think you should ask for permission from naruse before you commit</p>
Ruby master - Feature #9239: Array#to_h ignores flat arrayshttps://bugs.ruby-lang.org/issues/9239?journal_id=437492013-12-19T00:06:28Zsawa (Tsuyoshi Sawada)
<ul></ul><p>marcandre (Marc-Andre Lafortune),</p>
<p>Thanks for the patch to reflect my request, and thanks for ALREADY HAVING ASKED Naruse-san for the commit.</p>
Ruby master - Feature #9239: Array#to_h ignores flat arrayshttps://bugs.ruby-lang.org/issues/9239?journal_id=437572013-12-19T14:26:36Zmarcandre (Marc-Andre Lafortune)ruby-core@marc-andre.ca
<ul><li><strong>Assignee</strong> changed from <i>marcandre (Marc-Andre Lafortune)</i> to <i>naruse (Yui NARUSE)</i></li><li><strong>Priority</strong> changed from <i>Normal</i> to <i>6</i></li></ul> Ruby master - Feature #9239: Array#to_h ignores flat arrayshttps://bugs.ruby-lang.org/issues/9239?journal_id=437842013-12-21T01:02:40Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>As Array#to_h says and test_to_h describes, Array#to_h is different from Hash[].<br>
The behavior is expected one.</p>
<p>Returns the result of interpreting ary as an array of<br>
[key, value] pairs. Elements other than pairs of<br>
values are ignored.</p>
Ruby master - Feature #9239: Array#to_h ignores flat arrayshttps://bugs.ruby-lang.org/issues/9239?journal_id=437852013-12-21T03:05:08Zmarcandre (Marc-Andre Lafortune)ruby-core@marc-andre.ca
<ul></ul><p>Yui, I&#39;m wondering if you actually read the whole thread. In any case, I probably don&#39;t have to point out that Matz usually has the privilege of rejecting feature requests.</p>
<p>I&#39;ve summarized it as <a class="issue tracker-4 status-5 priority-4 priority-default closed" title="Backport: Array#to_h should not ignore badly formed elements (Closed)" href="https://bugs.ruby-lang.org/issues/9270">#9270</a>.</p>