[ruby-core:70852] [Ruby trunk - Feature #9970] Add `Hash#map_keys` and `Hash#map_values`

From: matz@...
Date: 2015-09-18 08:59:36 UTC
List: ruby-core #70852
Issue #9970 has been updated by Yukihiro Matsumoto.

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.


----------------------------------------
Feature #9970: Add `Hash#map_keys` and `Hash#map_values`
https://bugs.ruby-lang.org/issues/9970#change-54225

* Author: Sean Griffin
* Status: Open
* Priority: Normal
* Assignee: Nobuyoshi Nakada
----------------------------------------
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)


-- 
https://bugs.ruby-lang.org/

In This Thread

Prev Next