Feature #7793

New methods on Hash

Added by dsisnero (Dominic Sisneros) over 4 years ago. Updated about 2 years ago.

Target version:


It would be nice to have the following methods added to hash

h = { name: 'dominic', request: 'add the following methods', :why => 'convenience'}

h.map_v{|v| v.upcase}
#=> {:name=>"DOMINIC", :request=>"ADD THE FOLLOWING METHODS", :why=>"CONVENIENCE"}

h.map_k{|k| k.to_s}
#=> { "name"=> 'dominic', "request"=>"add the following methods', "why" => "convenience"}

h.map_kv{|k,v| [k.to_s, v.upcase]}
#=> { "name"=>"DOMINIC", "request"=>"ADD THE FOLLOWING METHODS", "why"=>"CONVENIENCE"}

class Hash

  def map_v
    reduce({}) do |result, array|
      k,v = array
      new_val = yield v
      result.merge( k => new_val)

  def map_k
    reduce({}) do |result, array|
      k,v = array
      new_k = yield k
      result.merge(new_k => v)

  def map_kv
    reduce({}) do |result, array|
      new_k,new_v = yield array
      result.merge(new_k => new_v)


Related issues

Related to Ruby trunk - Feature #6669: A method like Hash#map but returns hashFeedback2012-06-30
Related to Ruby trunk - Feature #4151: Enumerable#categorizeAssigned
Related to Ruby trunk - Feature #7292: Enumerable#to_hClosed2012-11-07
Related to Ruby trunk - Feature #10552: [PATCH] Add Enumerable#frequencies and Enumerable#relative_frequenciesOpen2014-11-27
Related to Ruby trunk - Feature #12512: Import Hash#transform_values and its destructive version from ActiveSupportClosed
Has duplicate Ruby trunk - Feature #9970: Add `Hash#map_keys` and `Hash#map_values`Open


#1 [ruby-core:51934] Updated by marcandre (Marc-Andre Lafortune) over 4 years ago

  • Status changed from Open to Closed

I am glad to see that more people like you take the time to propose ways to create hashes.

I completely agree that hash creation from Enumerable is lacking currently.

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:

If you have the time, read on those and see if you can contribute.


#2 [ruby-core:51945] Updated by dsisnero (Dominic Sisneros) over 4 years ago

This should be re-opened. It is not for all enumerables but only for hash.

map_v and map_k are very useful

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

#3 [ruby-core:51947] Updated by marcandre (Marc-Andre Lafortune) over 4 years ago

  • Category set to core
  • Status changed from Closed to Assigned
  • Assignee set to matz (Yukihiro Matsumoto)

Fine, I'll reopen and assign this to Matz.

#4 [ruby-core:51948] Updated by charliesome (Charlie Somerville) over 4 years ago

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.

#5 [ruby-core:51957] Updated by nobu (Nobuyoshi Nakada) over 4 years ago

Considering existing methods:

$ ruby -e 'p Hash.instance_methods(false).grep(/each_/)'
[:each_value, :each_key, :each_pair]

They should be map_key, may_value, and map_pair, respectively, I

Anyway, why don't you make it a gem first?

#6 [ruby-core:51958] Updated by nobu (Nobuyoshi Nakada) over 4 years ago

  • Description updated (diff)

#7 [ruby-core:52193] Updated by yhara (Yutaka HARA) over 4 years ago

  • Target version set to next minor

#8 [ruby-core:53356] Updated by phluid61 (Matthew Kerwin) over 4 years ago

nobu (Nobuyoshi Nakada) wrote:

Anyway, why don't you make it a gem first?

That's a good idea. Let's see what the uptake is, if any:

Note: I used #map_keys, #map_values and #map_pairs as my method names.

#9 [ruby-core:63197] Updated by Ajedi32 (Andrew M) about 3 years ago

FYI, Rails has a method similar to the proposed map_k called transform_keys.

#10 [ruby-core:63417] Updated by nobu (Nobuyoshi Nakada) about 3 years ago

  • Has duplicate Feature #9970: Add `Hash#map_keys` and `Hash#map_values` added

#11 [ruby-core:64810] Updated by nobu (Nobuyoshi Nakada) almost 3 years ago

  • Description updated (diff)

#12 [ruby-core:64814] Updated by trans (Thomas Sawyer) almost 3 years ago

An issue with the name is that "map" semantically means to create an Array,{ |k,v| ... } produces an Array. So map_keys would make sense to mean ahash.map_keys{ |k| ... } and produce an Array too. Hash#map_pair would just a synonym for #map, just as #each_pair is just a synonym for #each.

Facets has long had Hash#rekey and Hash#revalue (and in-place forms Hash#rekey! and Hash#revalue!). These names are concise and do not suffer this semantic issue. Note Facets doesn't have a #remap 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.

#13 [ruby-core:65509] Updated by sawa (Tsuyoshi Sawada) almost 3 years ago

Just like there are map and map!, there should be both a non-desctructive and a destructive version for this method.

h = {a: "foo"}

h.non_destructive_one{|k, v| [k.to_s, v.upcase]} #=> {"a" => "FOO"}
h #=> {a: "foo"}

h.destructive_one!{|k, v| [k.to_s, v.upcase]} #=> {"a" => "FOO"}
h #=> {"a" => "FOO"}

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.

h.non_destructive_one{|k, v| {k.to_s => v.upcase}} #=> {"a" => "FOO"}
h.destructive_one!{|k, v| {k.to_s => v.upcase}} #=> {"a" => "FOO"}

#14 [ruby-core:65513] Updated by avit (Andrew Vit) almost 3 years ago

the block for these methods should take a hash rather than an array.

Do you mean the input should be a single argument with a hash? I don't think that is very consistent for |k, v| expansion.

That should make more sense since the return value is a hash.

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.

#15 [ruby-core:65994] Updated by Ajedi32 (Andrew M) over 2 years ago

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.

Option 1

The original proposal, uses the term map to express changing the keys or values on the hash, and keeps things terse by abbreviating the terms 'key' and 'value':

  • Hash#map_k
  • Hash#map_k!
  • Hash#map_vs
  • Hash#map_v!
  • Hash#map_kv
  • Hash#map_kv!

Option 2

A clearer, more verbose alternative to option 1. (Proposed by Charlie Somerville.)

Option 3

Given the existing methods Hash#each_key, Hash#each_value, and Hash#each_pair, it might be better to use a sigular alternative to option 2. (Proposed by Nobuyoshi Nakada.)

  • Hash#map_key
  • Hash#map_key!
  • Hash#map_value
  • Hash#map_value!
  • Hash#map_pair
  • Hash#map_pair!

Option 4

Given the potential for the previous options to be confused with Hash#map, which returns an array, it might be best to use an entirely different naming convention. This one is based on Facets, a popular (485,329 downloads on Rubygems) library with the purpose of extending Ruby's core classes with useful methods. (Proposed by Thomas Sawyer.)

Option 5

Similar to option 4, but based on the naming convention used by Ruby on Rails.

#16 [ruby-core:66051] Updated by trans (Thomas Sawyer) over 2 years ago

I can't help but mention it, because it gave me a chuckle....

I like rekey and revalue from #4, because they make sense semantically, don't confuse the idea of map returning an array, and they are concise. Concision is always a big plus. However graph and mash don't really convey much in their names (mash 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.

Options #2 and #5 are nice for their consistency --the use of _keys, _values and _pairs-- But they lack for concision (especially #5) which sucks, and #2 has the map name issue as mentioned.

So I tried a combination of both ideas using re- as the prefix to the three suffixes and got:

  • rekey
  • revalue
  • repair

At which point the giggles kicked in :-)

#17 [ruby-core:66055] Updated by Ajedi32 (Andrew M) over 2 years ago

"repair"? Hehe, yeah that's kind of an unfortunate coincidence.

The thing I really like about Option 4's graph and mash is that they are methods on Enumerable, 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 #6669. Right now, a similar effect can be achieved (for the non-destructive method anyway) by chaining map and to_h, so perhaps the full hash transform methods don't provide as big of a benefit over what we have now as rekey and revalue do.

If we do decide to base our names off of the assumption that the full hash transform methods will be on Enumerable, and not Hash, then perhaps something like this might work:

  • Enumerable#associate
  • Hash#associate!

#18 [ruby-core:66563] Updated by duerst (Martin Dürst) over 2 years ago

  • Related to Feature #10552: [PATCH] Add Enumerable#frequencies and Enumerable#relative_frequencies added

#19 [ruby-core:69172] Updated by rafaelfranca (Rafael França) about 2 years ago

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 rekey and revalue does.

#20 Updated by mrkn (Kenta Murata) about 1 year ago

  • Related to Feature #12512: Import Hash#transform_values and its destructive version from ActiveSupport added

Also available in: Atom PDF