Project

General

Profile

Feature #9970

Add `Hash#map_keys` and `Hash#map_values`

Added by seantheprogrammer (Sean Griffin) about 3 years ago. Updated 3 months ago.

Status:
Open
Priority:
Normal
Target version:
-
[ruby-core:63270]

Description

These methods simplify two common patterns when working with hashes, and transforming the data.

Without map_keys:

Hash[{ a: 1, b: 2 }.map { |key, value| [key.to_s, value] }]

With map_keys:

{ a: 1, b: 2 }.map_keys(&:to_s)

Without map_values:

Hash[{ a: '1', b: '2' }.map { |key, value| [key, value.to_i] }]

With map_values:

{ a: '1', b: '2' }.map_values(&:to_i)

The patch, with tests, is attached.

hash-map-keys-and-map-values.diff (5.79 KB) hash-map-keys-and-map-values.diff seantheprogrammer (Sean Griffin), 06/21/2014 09:34 PM

Related issues

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
Is duplicate of Ruby trunk - Feature #7793: New methods on HashAssigned

History

#2 [ruby-core:63273] Updated by seantheprogrammer (Sean Griffin) about 3 years ago

Matthew Kerwin wrote:

Duplicate of #7793 ?

--
Matthew Kerwin
http://matthew.kerwin.net.au/

It looks like that was a feature request, and not a patch?

#4 [ruby-core:63397] Updated by phluid61 (Matthew Kerwin) about 3 years ago

A patch that introduces a new feature is still a feature request, and the feature you'd introducing matches one of the stronger suggestions in #7793.

And considering the other ticket has been sitting for a year now, I suspect this is a low priority. :\

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

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

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

#7 Updated by matz (Yukihiro Matsumoto) almost 2 years ago

  • Assignee set to nobu (Nobuyoshi Nakada)

Hash#map_keys and Hash#map_values may cause confusion. They may have impression of collection of transformed {keys,values}.

But I agree with usefulness of the proposed feature. According to #7793 Hash#transform_keys and Hash#transform_values seem reasonable.
Besides that, AcriveSupport also provides transform_keys and transform_values.

Matz.

#8 Updated by seantheprogrammer (Sean Griffin) almost 2 years ago

Just to be clear, are you saying you would prefer that I edit the patch so that the methods are named #transform_keys and #transform_values?

#9 Updated by matz (Yukihiro Matsumoto) almost 2 years ago

No, you don't have to. We will take care. Thank you for the proposal.

Matz.

#10 Updated by shyouhei (Shyouhei Urabe) about 1 year ago

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

#11 [ruby-core:81220] Updated by graywolf (Gray Wolf) 3 months ago

What's the status on this? In #12512, only Hash#transform_values was added, should I bother making patch adding Hash#transform_keys (and maybe Hash#transform_pairs)? Or is this (#9970) still in the process based on last comment from matz ("We will take care [of it].")?

W.

#12 [ruby-core:81221] Updated by shyouhei (Shyouhei Urabe) 3 months ago

This issue has no move so far. I think you need to start discussing about transform_keys if you want that. It has one problem though; what happens when the transformed keys conflict is not intuitive. Is that an exception? Or the latter wins?

{ x: 1, y: 2 }.transform_keys {|*| :same_key } # should what happen?

#13 [ruby-core:81229] Updated by graywolf (Gray Wolf) 3 months ago

I think emulating the behaviour of

[2] pry(main)> Hash[{ a: 1, b: 2 }.map { |key, value| [:s, value] }]
=> {:s=>2}

is reasonable. It's also the same thing Hash#transform_keys from rails does (afaict).

You said

you need to start discussing about transform_keys if you want that

ok, how do I do that? Here? IRC? Mailing list (which one)?

#14 [ruby-core:81233] Updated by shyouhei (Shyouhei Urabe) 3 months ago

graywolf (Gray Wolf) wrote:

You said

you need to start discussing about transform_keys if you want that

ok, how do I do that? Here? IRC? Mailing list (which one)?

Thank you, please file a separate issue that requests this method.

Also available in: Atom PDF