Issue #9970 has been updated by graywolf (Gray Wolf).


I think emulating the behaviour of

```ruby
[2] pry(main)> Hash[{ a: 1, b: 2 }.map { |key, value| [:s, value] }]
=> {:s=>2}
```

is reasonable. It's also the same thing `Hash#transform_keys` from rails does (afaict).

You said

> you need to start discussing about transform_keys if you want that

ok, how do I do that? Here? IRC? Mailing list (which one)?

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

* Author: seantheprogrammer (Sean Griffin)
* Status: Open
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
* Target version: 
----------------------------------------
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/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>