https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112017-05-19T18:52:38ZRuby Issue Tracking SystemRuby master - Feature #13583: Adding `Hash#transform_keys` methodhttps://redmine.ruby-lang.org/issues/13583?journal_id=649612017-05-19T18:52:38Zgraywolf (Gray Wolf)
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/64961/diff?detail_id=45158">diff</a>)</li></ul> Ruby master - Feature #13583: Adding `Hash#transform_keys` methodhttps://redmine.ruby-lang.org/issues/13583?journal_id=649632017-05-19T18:53:31Zgraywolf (Gray Wolf)
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/64963/diff?detail_id=45160">diff</a>)</li></ul> Ruby master - Feature #13583: Adding `Hash#transform_keys` methodhttps://redmine.ruby-lang.org/issues/13583?journal_id=649712017-05-20T01:49:33Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>Thank you for issuing this. I see there is an obvious needs for this transformation (stringify_keys) so I'm :+1: to the feature.</p>
<p>Let's have a concrete definition of this requested method:</p>
<ul>
<li>Its name is Hash#transform_keys.</li>
<li>It returns a newly created Hash instance.</li>
<li>It has zero arity.</li>
<li>It yields,
<ul>
<li>with only one block parameter (which is a key of the original hash),</li>
<li>the evaluated value is the new key for the entry.</li>
</ul>
</li>
<li>When the new key conflicts, later entry silently discards former entry (see the description of this issue).</li>
</ul>
<p>Is it okey? Am I missing something? Do people have any opinion?</p> Ruby master - Feature #13583: Adding `Hash#transform_keys` methodhttps://redmine.ruby-lang.org/issues/13583?journal_id=649792017-05-20T08:56:38Zgraywolf (Gray Wolf)
<ul></ul><p>I don't think you missed anything, except I would just point out to also add <code>Hash#transform_keys!</code>. I don't know if it's worth mentioning or just kinda automatically assumed.</p> Ruby master - Feature #13583: Adding `Hash#transform_keys` methodhttps://redmine.ruby-lang.org/issues/13583?journal_id=649972017-05-20T20:01:47Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>I think the names are good, both #transform_keys and #transform_values.</p>
<p>Seem quite clear to me from the names.</p>
<p>On the linked older issue (-3 years), the names were different,<br>
Hash#map_keys and Hash#map_values. Matz said that the names may<br>
be confusing. Perhaps #transform_keys and #transform_values<br>
are better names. (I have not checked if the proposal is the<br>
very same; shyouhei provided a very specific definition here,<br>
including behaviour such as arity and yield-situations, which<br>
I think the other proposal did not have). Guess matz will have<br>
a look.</p>
<p>graywolf, could you perhaps show some example documentation for<br>
the two methods?</p> Ruby master - Feature #13583: Adding `Hash#transform_keys` methodhttps://redmine.ruby-lang.org/issues/13583?journal_id=657702017-07-14T05:33:56Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>Looks good to me.</p>
<p>Matz.</p> Ruby master - Feature #13583: Adding `Hash#transform_keys` methodhttps://redmine.ruby-lang.org/issues/13583?journal_id=657762017-07-14T06:44:06Zmrkn (Kenta Murata)muraken@gmail.com
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset trunk|r59328.</p>
<hr>
<p>hash.c: Add Hash#transform_keys and Hash#transform_keys!</p>
<ul>
<li>
<p>hash.c (transform_keys_i, rb_hash_transform_keys): Add Hash#transform_keys.<br>
[Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Adding `Hash#transform_keys` method (Closed)" href="https://redmine.ruby-lang.org/issues/13583">#13583</a>] <a href="/issues/13583">[ruby-core:81290]</a></p>
</li>
<li>
<p>hash.c (rb_hash_transform_keys_bang): Add Hash#transform_keys!.<br>
[Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Adding `Hash#transform_keys` method (Closed)" href="https://redmine.ruby-lang.org/issues/13583">#13583</a>] <a href="/issues/13583">[ruby-core:81290]</a></p>
</li>
<li>
<p>test/ruby/test_hash.rb: Add tests for above changes.</p>
</li>
</ul> Ruby master - Feature #13583: Adding `Hash#transform_keys` methodhttps://redmine.ruby-lang.org/issues/13583?journal_id=682792017-12-11T21:28:13Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Status</strong> changed from <i>Closed</i> to <i>Open</i></li><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li></ul><p>I'm not sure I like the current behavior of <code>transform_keys!</code>.</p>
<p>Two possibilities: <code>transform_keys!</code> is <code>each_key { delete(old_key), set(new_key) }</code> (as is currently) or <code>replace(transform_keys)</code> (I think I prefer this).</p>
<p>Matz, could you confirm what behavior you want?</p>
<p>Current:</p>
<pre><code>h = {1 => :hello, 2 => 'world'}
h.transform_keys(&:succ) # => {2 => :hello, 3 => 'world'}
h.transform_keys!(&:succ) # => {3 => :hello}
</code></pre>
<p>With using <code>replace</code>, we'd get the same results.</p>
<p>The current behavior allows partial updates though:</p>
<pre><code>h = {1 => :hello, 2 => :world}
h.transform_keys! { |k| k == 1 ? :one : break }
h # => {2 => world, :one => :hello}
</code></pre>
<p>With the <code>replace</code> version, <code>h</code> would be unchanged (or else we'd have to write an <code>ensure</code> to do the partial update)</p> Ruby master - Feature #13583: Adding `Hash#transform_keys` methodhttps://redmine.ruby-lang.org/issues/13583?journal_id=682822017-12-11T22:07:05Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Nevermind, I just remembered that ActiveSupport also defines <code>transform_keys!</code>, so best match its behavior.</p>