Project

General

Profile

Actions

Feature #9970

closed

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

Added by seantheprogrammer (Sean Griffin) over 10 years ago. Updated about 3 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
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0