Feature #9970
closedAdd `Hash#map_keys` and `Hash#map_values`
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
Updated by phluid61 (Matthew Kerwin) over 10 years ago
Duplicate of #7793 ?
--
Matthew Kerwin
http://matthew.kerwin.net.au/
Updated by seantheprogrammer (Sean Griffin) over 10 years 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?
Updated by seantheprogrammer (Sean Griffin) over 10 years ago
Ping
Updated by phluid61 (Matthew Kerwin) over 10 years 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. :\
Updated by nobu (Nobuyoshi Nakada) over 10 years ago
- Is duplicate of Feature #7793: New methods on Hash added
Updated by duerst (Martin Dürst) about 10 years ago
- Related to Feature #10552: [PATCH] Add Enumerable#frequencies and Enumerable#relative_frequencies added
Updated by matz (Yukihiro Matsumoto) over 9 years ago
- Assignee set to nobu (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.
Updated by seantheprogrammer (Sean Griffin) about 9 years ago
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?
Updated by matz (Yukihiro Matsumoto) about 9 years ago
No, you don't have to. We will take care. Thank you for the proposal.
Matz.
Updated by shyouhei (Shyouhei Urabe) over 8 years ago
- Related to Feature #12512: Import Hash#transform_values and its destructive version from ActiveSupport added
Updated by graywolf (Gray Wolf) over 7 years ago
Updated by shyouhei (Shyouhei Urabe) over 7 years ago
This issue has no move so far. I think you need to start discussing about transform_keys if you want that. It has one problem though; what happens when the transformed keys conflict is not intuitive. Is that an exception? Or the latter wins?
{ x: 1, y: 2 }.transform_keys {|*| :same_key } # should what happen?
Updated by graywolf (Gray Wolf) over 7 years ago
I think emulating the behaviour of
[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)?
Updated by shyouhei (Shyouhei Urabe) over 7 years ago
graywolf (Gray Wolf) wrote:
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)?
Thank you, please file a separate issue that requests this method.
Updated by jeremyevans0 (Jeremy Evans) about 3 years ago
- Status changed from Open to Closed
This feature was implemented as transform_keys
, transform_values
, and to_h
.