https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112013-02-07T02:00:07ZRuby Issue Tracking SystemRuby master - Feature #7793: New methods on Hashhttps://redmine.ruby-lang.org/issues/7793?journal_id=359322013-02-07T02:00:07Zmarcandre (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>I am glad to see that more people like you take the time to propose ways to create hashes.</p>
<p>I completely agree that hash creation from Enumerable is lacking currently.</p>
<p>I will close this feature request because I am convinced it can't be accepted as is (the proposed names have no chance of being accepted) and because it is largely duplicated by the following:</p>
<p><a href="https://bugs.ruby-lang.org/issues/6669" class="external">https://bugs.ruby-lang.org/issues/6669</a><br>
<a href="https://bugs.ruby-lang.org/issues/4151" class="external">https://bugs.ruby-lang.org/issues/4151</a><br>
<a href="https://bugs.ruby-lang.org/issues/7292" class="external">https://bugs.ruby-lang.org/issues/7292</a></p>
<p>If you have the time, read on those and see if you can contribute.</p>
<p>Thanks</p> Ruby master - Feature #7793: New methods on Hashhttps://redmine.ruby-lang.org/issues/7793?journal_id=359442013-02-07T05:02:04Zdsisnero (Dominic Sisneros)dsisnero@gmail.com
<ul></ul><p>This should be re-opened. It is not for all enumerables but only for hash.</p>
<p>map_v and map_k are very useful</p>
<p>map_kv is similar to h.mash and others and could be eliminated by those other bugs but the other functions aren't and are specifically for hashes and thus this should be re-opened</p> Ruby master - Feature #7793: New methods on Hashhttps://redmine.ruby-lang.org/issues/7793?journal_id=359462013-02-07T06:51:53Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Category</strong> set to <i>core</i></li><li><strong>Status</strong> changed from <i>Closed</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li></ul><p>Fine, I'll reopen and assign this to Matz.</p> Ruby master - Feature #7793: New methods on Hashhttps://redmine.ruby-lang.org/issues/7793?journal_id=359472013-02-07T06:59:25ZAnonymous
<ul></ul><p>At the risk of bike shedding, I think map_k and map_v should be named map_keys and map_values. That can be for matz to decide though.</p> Ruby master - Feature #7793: New methods on Hashhttps://redmine.ruby-lang.org/issues/7793?journal_id=359552013-02-07T10:14:09Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>Considering existing methods:</p>
<pre><code>$ ruby -e 'p Hash.instance_methods(false).grep(/each_/)'
[:each_value, :each_key, :each_pair]
</code></pre>
<p>They should be <code>map_key</code>, <code>may_value</code>, and <code>map_pair</code>, respectively, I<br>
think.</p>
<p>Anyway, why don't you make it a gem first?</p> Ruby master - Feature #7793: New methods on Hashhttps://redmine.ruby-lang.org/issues/7793?journal_id=359562013-02-07T10:18:10Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/35956/diff?detail_id=25547">diff</a>)</li></ul> Ruby master - Feature #7793: New methods on Hashhttps://redmine.ruby-lang.org/issues/7793?journal_id=362142013-02-13T18:22:18Zyhara (Yutaka HARA)
<ul><li><strong>Target version</strong> set to <i>2.6</i></li></ul> Ruby master - Feature #7793: New methods on Hashhttps://redmine.ruby-lang.org/issues/7793?journal_id=375532013-03-13T11:13:16Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>nobu (Nobuyoshi Nakada) wrote:</p>
<blockquote>
<p>Anyway, why don't you make it a gem first?</p>
</blockquote>
<p>That's a good idea. Let's see what the uptake is, if any: <a href="https://rubygems.org/gems/hashmap" class="external">https://rubygems.org/gems/hashmap</a></p>
<p>Note: I used #map_keys, #map_values and #map_pairs as my method names.</p> Ruby master - Feature #7793: New methods on Hashhttps://redmine.ruby-lang.org/issues/7793?journal_id=472482014-06-16T15:49:46ZAjedi32 (Andrew M)andrewm.bpi@gmail.com
<ul></ul><p>FYI, Rails has a method similar to the proposed <code>map_k</code> called <a href="http://api.rubyonrails.org/classes/Hash.html#method-i-transform_keys-21" class="external"><code>transform_keys</code></a>.</p> Ruby master - Feature #7793: New methods on Hashhttps://redmine.ruby-lang.org/issues/7793?journal_id=474592014-06-30T04:22:06Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Has duplicate</strong> <i><a class="issue tracker-2 status-5 priority-4 priority-default closed" href="/issues/9970">Feature #9970</a>: Add `Hash#map_keys` and `Hash#map_values`</i> added</li></ul> Ruby master - Feature #7793: New methods on Hashhttps://redmine.ruby-lang.org/issues/7793?journal_id=486912014-09-06T11:14:03Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/48691/diff?detail_id=35195">diff</a>)</li></ul> Ruby master - Feature #7793: New methods on Hashhttps://redmine.ruby-lang.org/issues/7793?journal_id=486952014-09-06T16:14:46Ztrans (Thomas Sawyer)
<ul></ul><p>An issue with the name is that "map" semantically means to create an Array, i.e.<code>ahash.map{ |k,v| ... }</code> produces an Array. So <code>map_keys</code> would make sense to mean <code>ahash.map_keys{ |k| ... }</code> and produce an Array too. <code>Hash#map_pair</code> would just a synonym for <code>#map</code>, just as <code>#each_pair</code> is just a synonym for <code>#each</code>.</p>
<p>Facets has long had <code>Hash#rekey</code> and <code>Hash#revalue</code> (and in-place forms <code>Hash#rekey!</code> and <code>Hash#revalue!</code>). These names are concise and do not suffer this semantic issue. Note Facets doesn't have a <code>#remap</code> method (though I suppose it could) because it has Enumerable#mash, and it's alias #graph, which can create a Hash from any Enumerable object.</p> Ruby master - Feature #7793: New methods on Hashhttps://redmine.ruby-lang.org/issues/7793?journal_id=492842014-10-08T04:17:37Zsawa (Tsuyoshi Sawada)
<ul></ul><p>Just like there are <code>map</code> and <code>map!</code>, there should be both a non-desctructive and a destructive version for this method.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">h</span> <span class="o">=</span> <span class="p">{</span><span class="ss">a: </span><span class="s2">"foo"</span><span class="p">}</span>
<span class="n">h</span><span class="p">.</span><span class="nf">non_destructive_one</span><span class="p">{</span><span class="o">|</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="o">|</span> <span class="p">[</span><span class="n">k</span><span class="p">.</span><span class="nf">to_s</span><span class="p">,</span> <span class="n">v</span><span class="p">.</span><span class="nf">upcase</span><span class="p">]}</span> <span class="c1">#=> {"a" => "FOO"}</span>
<span class="n">h</span> <span class="c1">#=> {a: "foo"}</span>
<span class="n">h</span><span class="p">.</span><span class="nf">destructive_one!</span><span class="p">{</span><span class="o">|</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="o">|</span> <span class="p">[</span><span class="n">k</span><span class="p">.</span><span class="nf">to_s</span><span class="p">,</span> <span class="n">v</span><span class="p">.</span><span class="nf">upcase</span><span class="p">]}</span> <span class="c1">#=> {"a" => "FOO"}</span>
<span class="n">h</span> <span class="c1">#=> {"a" => "FOO"}</span>
</code></pre>
<p>I also have a (not that strong) opinion that the block for these methods should take a hash rather than an array. That should make more sense since the return value is a hash.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">h</span><span class="p">.</span><span class="nf">non_destructive_one</span><span class="p">{</span><span class="o">|</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="o">|</span> <span class="p">{</span><span class="n">k</span><span class="p">.</span><span class="nf">to_s</span> <span class="o">=></span> <span class="n">v</span><span class="p">.</span><span class="nf">upcase</span><span class="p">}}</span> <span class="c1">#=> {"a" => "FOO"}</span>
<span class="n">h</span><span class="p">.</span><span class="nf">destructive_one!</span><span class="p">{</span><span class="o">|</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="o">|</span> <span class="p">{</span><span class="n">k</span><span class="p">.</span><span class="nf">to_s</span> <span class="o">=></span> <span class="n">v</span><span class="p">.</span><span class="nf">upcase</span><span class="p">}}</span> <span class="c1">#=> {"a" => "FOO"}</span>
</code></pre> Ruby master - Feature #7793: New methods on Hashhttps://redmine.ruby-lang.org/issues/7793?journal_id=492882014-10-08T07:11:08Zavit (Andrew Vit)andrew@avit.ca
<ul></ul><blockquote>
<p>the block for these methods should take a hash rather than an array.</p>
</blockquote>
<p>Do you mean the input should be a single argument with a hash? I don't think that is very consistent for <code>|k, v|</code> expansion.</p>
<blockquote>
<p>That should make more sense since the return value is a hash.</p>
</blockquote>
<p>Everything inside the block is a tuple; what type the input/output are transformed from/to happens outside the block. IMHO the array makes more sense than the hash inside the block.</p> Ruby master - Feature #7793: New methods on Hashhttps://redmine.ruby-lang.org/issues/7793?journal_id=497272014-10-29T13:56:12ZAjedi32 (Andrew M)andrewm.bpi@gmail.com
<ul></ul><p>Below is a summary of the different naming proposals so far in this thread, with links to the documentation for real-world implementations where available.</p>
<a name="Option-1"></a>
<h1 >Option 1<a href="#Option-1" class="wiki-anchor">¶</a></h1>
<p>The original proposal, uses the term <code>map</code> to express changing the keys or values on the hash, and keeps things terse by abbreviating the terms 'key' and 'value':</p>
<ul>
<li><code>Hash#map_k</code></li>
<li><code>Hash#map_k!</code></li>
<li><code>Hash#map_vs</code></li>
<li><code>Hash#map_v!</code></li>
<li><code>Hash#map_kv</code></li>
<li><code>Hash#map_kv!</code></li>
</ul>
<a name="Option-2"></a>
<h1 >Option 2<a href="#Option-2" class="wiki-anchor">¶</a></h1>
<p>A clearer, more verbose alternative to option 1. (Proposed by Charlie Somerville.)</p>
<ul>
<li><a href="http://www.rubydoc.info/github/phluid61/hashmap-gem/master/Hash#map_keys-instance_method" class="external"><code>Hash#map_keys</code></a></li>
<li><a href="http://www.rubydoc.info/github/phluid61/hashmap-gem/master/Hash#map_keys%21-instance_method" class="external"><code>Hash#map_keys!</code></a></li>
<li><a href="http://www.rubydoc.info/github/phluid61/hashmap-gem/master/Hash#map_values-instance_method" class="external"><code>Hash#map_values</code></a></li>
<li><a href="http://www.rubydoc.info/github/phluid61/hashmap-gem/master/Hash#map_pairs%21-instance_method" class="external"><code>Hash#map_values!</code></a></li>
<li><a href="http://www.rubydoc.info/github/phluid61/hashmap-gem/master/Hash#map_pairs-instance_method" class="external"><code>Hash#map_pairs</code></a></li>
<li><a href="http://www.rubydoc.info/github/phluid61/hashmap-gem/master/Hash#map_pairs%21-instance_method" class="external"><code>Hash#map_pairs!</code></a></li>
</ul>
<a name="Option-3"></a>
<h1 >Option 3<a href="#Option-3" class="wiki-anchor">¶</a></h1>
<p>Given the existing methods <code>Hash#each_key</code>, <code>Hash#each_value</code>, and <code>Hash#each_pair</code>, it might be better to use a sigular alternative to option 2. (Proposed by Nobuyoshi Nakada.)</p>
<ul>
<li><code>Hash#map_key</code></li>
<li><code>Hash#map_key!</code></li>
<li><code>Hash#map_value</code></li>
<li><code>Hash#map_value!</code></li>
<li><code>Hash#map_pair</code></li>
<li><code>Hash#map_pair!</code></li>
</ul>
<a name="Option-4"></a>
<h1 >Option 4<a href="#Option-4" class="wiki-anchor">¶</a></h1>
<p>Given the potential for the previous options to be confused with <code>Hash#map</code>, which returns an array, it might be best to use an entirely different naming convention. This one is based on <a href="https://github.com/rubyworks/facets" class="external">Facets</a>, a popular (485,329 downloads on Rubygems) library with the purpose of extending Ruby's core classes with useful methods. (Proposed by Thomas Sawyer.)</p>
<ul>
<li><a href="http://www.rubydoc.info/github/rubyworks/facets/master/Hash#rekey-instance_method" class="external"><code>Hash#rekey</code></a></li>
<li><a href="http://www.rubydoc.info/github/rubyworks/facets/master/Hash#rekey%21-instance_method" class="external"><code>Hash#rekey!</code></a></li>
<li><a href="http://www.rubydoc.info/github/rubyworks/facets/master/Hash#revalue-instance_method" class="external"><code>Hash#revalue</code></a></li>
<li><a href="http://www.rubydoc.info/github/rubyworks/facets/master/Hash#revalue%21-instance_method" class="external"><code>Hash#revalue!</code></a></li>
<li>
<a href="http://www.rubydoc.info/github/rubyworks/facets/master/Enumerable#graph-instance_method" class="external"><code>Enumerable#graph</code></a> (See <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: A method like Hash#map but returns hash (Closed)" href="https://redmine.ruby-lang.org/issues/6669">#6669</a>)
<ul>
<li>Aliased as <code>Enumerable#mash</code>
</li>
</ul>
</li>
<li>
<a href="http://www.rubydoc.info/github/rubyworks/facets/master/Hash#graph%21-instance_method" class="external"><code>Hash#graph!</code></a>
<ul>
<li>Aliased as <code>Hash#mash!</code>
</li>
</ul>
</li>
</ul>
<a name="Option-5"></a>
<h1 >Option 5<a href="#Option-5" class="wiki-anchor">¶</a></h1>
<p>Similar to option 4, but based on the naming convention used by Ruby on Rails.</p>
<ul>
<li><a href="http://api.rubyonrails.org/classes/Hash.html#method-i-transform_keys" class="external"><code>Hash#transform_keys</code></a></li>
<li><a href="http://api.rubyonrails.org/classes/Hash.html#method-i-transform_keys-21" class="external"><code>Hash#transform_keys!</code></a></li>
<li><code>Hash#transform_values</code></li>
<li><code>Hash#transform_values!</code></li>
<li><code>Hash#transform_pairs</code></li>
<li><code>Hash#transform_pairs!</code></li>
</ul> Ruby master - Feature #7793: New methods on Hashhttps://redmine.ruby-lang.org/issues/7793?journal_id=497722014-11-02T17:19:23Ztrans (Thomas Sawyer)
<ul></ul><p>I can't help but mention it, because it gave me a chuckle....</p>
<p>I like <code>rekey</code> and <code>revalue</code> from #4, because they make sense semantically, don't confuse the idea of <code>map</code> returning an array, and they are <em>concise</em>. Concision is always a big plus. However <code>graph</code> and <code>mash</code> don't really convey much in their names (<code>mash</code> is combination of "map" and "hash" btw), so I've always been rather ho-hum about those, but never could come up with a better, yet still concise, alternative.</p>
<p>Options #2 and #5 are nice for their consistency --the use of <code>_keys</code>, <code>_values</code> and <code>_pairs</code>-- But they lack for concision (especially #5) which sucks, and #2 has the map name issue as mentioned.</p>
<p>So I tried a combination of both ideas using <code>re-</code> as the prefix to the three suffixes and got:</p>
<ul>
<li><code>rekey</code></li>
<li><code>revalue</code></li>
<li><code>repair</code></li>
</ul>
<p>At which point the giggles kicked in :-)</p> Ruby master - Feature #7793: New methods on Hashhttps://redmine.ruby-lang.org/issues/7793?journal_id=497762014-11-02T19:23:04ZAjedi32 (Andrew M)andrewm.bpi@gmail.com
<ul></ul><p>"repair"? Hehe, yeah that's kind of an unfortunate coincidence.</p>
<p>The thing I really like about Option 4's <code>graph</code> and <code>mash</code> is that they are methods on <code>Enumerable</code>, which means they can be used with any Enumerable object, not just hashes. As I mentioned, the creation of a method like that is being discussed in <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: A method like Hash#map but returns hash (Closed)" href="https://redmine.ruby-lang.org/issues/6669">#6669</a>. Right now, a similar effect can be achieved (for the non-destructive method anyway) by chaining <code>map</code> and <code>to_h</code>, so perhaps the full hash transform methods don't provide as big of a benefit over what we have now as <code>rekey</code> and <code>revalue</code> do.</p>
<p>If we do decide to base our names off of the assumption that the full hash transform methods will be on <code>Enumerable</code>, and not <code>Hash</code>, then perhaps something like this might work:</p>
<ul>
<li><code>Enumerable#associate</code></li>
<li><code>Hash#associate!</code></li>
</ul> Ruby master - Feature #7793: New methods on Hashhttps://redmine.ruby-lang.org/issues/7793?journal_id=501762014-11-29T03:04:34Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-1 priority-4 priority-default" href="/issues/10552">Feature #10552</a>: [PATCH] Add Enumerable#frequencies and Enumerable#relative_frequencies</i> added</li></ul> Ruby master - Feature #7793: New methods on Hashhttps://redmine.ruby-lang.org/issues/7793?journal_id=524302015-05-13T18:12:42Zrafaelfranca (Rafael França)rafael@franca.dev
<ul></ul><p>I'm biased here since we already implemented part of Option #5 on Ruby on Rails but I prefer its explicitness over concision. It is not clear to me what <code>rekey</code> and <code>revalue</code> does.</p> Ruby master - Feature #7793: New methods on Hashhttps://redmine.ruby-lang.org/issues/7793?journal_id=592952016-06-21T14:02:52Zmrkn (Kenta Murata)muraken@gmail.com
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-5 priority-4 priority-default closed" href="/issues/12512">Feature #12512</a>: Import Hash#transform_values and its destructive version from ActiveSupport</i> added</li></ul> Ruby master - Feature #7793: New methods on Hashhttps://redmine.ruby-lang.org/issues/7793?journal_id=940392021-10-06T20:20:29Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Closed</i></li></ul><p>This feature was implemented as <code>transform_keys</code>, <code>transform_values</code>, and <code>to_h</code>.</p>