[#70843] Re: [ruby-cvs:58952] hsbt:r51801 (trunk): * lib/rubygems: Update to RubyGems HEAD(fe61e4c112). — Eric Wong <normalperson@...>
hsbt@ruby-lang.org wrote:
3 messages
2015/09/17
[ruby-core:70878] [Ruby trunk - Feature #9970] Add `Hash#map_keys` and `Hash#map_values`
From:
sean@...
Date:
2015-09-22 01:58:53 UTC
List:
ruby-core #70878
Issue #9970 has been updated by Sean Griffin.
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?
----------------------------------------
Feature #9970: Add `Hash#map_keys` and `Hash#map_values`
https://bugs.ruby-lang.org/issues/9970#change-54254
* 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/