Project

General

Profile

Actions

Feature #9970

closed

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

Added by seantheprogrammer (Sean Griffin) over 9 years ago. Updated over 2 years ago.

Status:
Closed
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 3 (1 open2 closed)

Related to Ruby master - Feature #10552: [PATCH] Add Enumerable#frequencies and Enumerable#relative_frequenciesOpen11/27/2014Actions
Related to Ruby master - Feature #12512: Import Hash#transform_values and its destructive version from ActiveSupportClosedmatz (Yukihiro Matsumoto)Actions
Is duplicate of Ruby master - Feature #7793: New methods on HashClosedmatz (Yukihiro Matsumoto)Actions

Updated by seantheprogrammer (Sean Griffin) over 9 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 9 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 9 years ago

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

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

Updated by matz (Yukihiro Matsumoto) over 8 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.

Actions #8

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

Actions #9

Updated by matz (Yukihiro Matsumoto) over 8 years ago

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

Matz.

Actions #10

Updated by shyouhei (Shyouhei Urabe) over 7 years ago

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

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

Actions #15

Updated by jeremyevans0 (Jeremy Evans) over 2 years ago

  • Status changed from Open to Closed

This feature was implemented as transform_keys, transform_values, and to_h.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0