From: shyouhei@... Date: 2017-05-20T01:49:34+00:00 Subject: [ruby-core:81298] [Ruby trunk Feature#13583] Adding `Hash#transform_keys` method Issue #13583 has been updated by shyouhei (Shyouhei Urabe). Thank you for issuing this. I see there is an obvious needs for this transformation (stringify_keys) so I'm :+1: to the feature. Let's have a concrete definition of this requested method: - Its name is Hash#transform_keys. - It returns a newly created Hash instance. - It has zero arity. - It yields, - with only one block parameter (which is a key of the original hash), - the evaluated value is the new key for the entry. - When the new key conflicts, later entry silently discards former entry (see the description of this issue). Is it okey? Am I missing something? Do people have any opinion? ---------------------------------------- Feature #13583: Adding `Hash#transform_keys` method https://bugs.ruby-lang.org/issues/13583#change-64971 * Author: graywolf (Gray Wolf) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- In 2.4, new useful method `Hash#transform_values` was added. I would like to propose also adding matching method `Hash#transform_keys`. ```ruby {a: 1, b: 2}.transform_keys { |k| k.to_s } => {"a"=>1, "b"=>2} ``` What needs to be considered is what to do in case of two keys mapping to the same new key ```ruby { a: 1, b: 2 }.transform_keys {|_| :same_key } # what should happen? ``` I think using `Hash[]` as model behaviour is a good idea. ```ruby Hash[{ a: 1, b: 2 }.map { |key, value| [:s, value] }] => {:s=>2} ``` it's also how `Hash#transform_keys` works in rails (afaict). This is a follow up feature request to #9970, which seems to be stalled. If the behaviour can be agreed upon, I can try putting together a patch (if no one else wants to step up). -- https://bugs.ruby-lang.org/ Unsubscribe: