Project

General

Profile

Feature #9970

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

Added by seantheprogrammer (Sean Griffin) almost 5 years ago. Updated almost 2 years 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.


Files

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_frequenciesOpen11/27/2014Actions
Related to Ruby trunk - Feature #12512: Import Hash#transform_values and its destructive version from ActiveSupportClosedActions
Is duplicate of Ruby trunk - Feature #7793: New methods on HashAssignedActions

History

Updated by seantheprogrammer (Sean Griffin) almost 5 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?

Updated by phluid61 (Matthew Kerwin) over 4 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. :\

Updated by nobu (Nobuyoshi Nakada) over 4 years ago

Updated by duerst (Martin Dürst) over 4 years ago

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

Updated by matz (Yukihiro Matsumoto) over 3 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) over 3 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) over 3 years ago

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

Matz.

#10

Updated by shyouhei (Shyouhei Urabe) almost 3 years ago

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

Updated by graywolf (Gray Wolf) almost 2 years 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.

Updated by shyouhei (Shyouhei Urabe) almost 2 years 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?

Updated by graywolf (Gray Wolf) almost 2 years 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)?

Updated by shyouhei (Shyouhei Urabe) almost 2 years 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