Feature #9970

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

Added by Sean Griffin 8 months ago. Updated 8 months ago.

[ruby-core:63270]
Status:Open
Priority:Normal
Assignee:-

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#relativ... Open 11/27/2014
Duplicates Ruby trunk - Feature #7793: New methods on Hash Assigned 02/07/2013

History

#1 Updated by Matthew Kerwin 8 months ago

Duplicate of #7793 ?

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

#2 Updated by Sean Griffin 8 months 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?

#3 Updated by Sean Griffin 8 months ago

Ping

#4 Updated by Matthew Kerwin 8 months 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 Updated by Nobuyoshi Nakada 8 months ago

#6 Updated by Martin Dürst 3 months ago

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

Also available in: Atom PDF