Project

General

Profile

Feature #9970

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

Added by Sean Griffin almost 2 years ago. Updated 7 months ago.

Status:
Open
Priority:
Normal
[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 Magnifier (5.79 KB) Sean Griffin, 06/21/2014 09:34 PM


Related issues

Related to Ruby trunk - Feature #10552: [PATCH] Add Enumerable#frequencies and Enumerable#relative_frequencies Open 11/27/2014
Duplicates Ruby trunk - Feature #7793: New methods on Hash Assigned

History

#1 [ruby-core:63271] Updated by Matthew Kerwin almost 2 years ago

Duplicate of #7793 ?

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

#2 [ruby-core:63273] Updated by Sean Griffin almost 2 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 Matthew Kerwin almost 2 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 Nobuyoshi Nakada almost 2 years ago

#6 [ruby-core:66561] Updated by Martin Dürst over 1 year ago

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

#7 Updated by Yukihiro Matsumoto 7 months ago

  • Assignee set to 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 Sean Griffin 7 months 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 Yukihiro Matsumoto 7 months ago

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

Matz.

Also available in: Atom PDF