Project

General

Profile

Feature #16274

Updated by sawa (Tsuyoshi Sawada) over 4 years ago

We have `Hash#transform_keys` and its bang version to change the keys of a hash, but that requires passing a block, which assumes that the mapping from old keys to new keys follow some rule. But in reality, we frequently want to change the keys where it is difficult to provide a rule. For example, suppose we have: 

 ``` 
 hash = {created: 2019-10-23 17:54:46 +0900, updated: 2019-10-23 17:59:18 +0900, author: "foo"} 
 ``` 

 and want to achieve: 

 ``` 
 {created_at: 2019-10-23 17:54:46 +0900, update_time: 2019-10-23 17:59:18 +0900, author: "foo"} 
 ``` 


 I request an option to change the keys of a hash not by giving a block, but by passing a hash. I came up with two options. 

 ### 1. Argument for `Hash#transform_keys` and its bang version 

 Allow `Hash#transform_keys` to optionally take a hash argument instead of a block. 

 ``` 
 hash.transform_values({created: :created_at, updated: :update_time}) 
 # => {created_at: 2019-10-23 17:54:46 +0900, update_time: 2019-10-23 17:59:18 +0900, author: "foo"} 
 ``` 

 ### 2. Argument for `Hash#slice` and the counterparts in other classes 

 Since `Hash#slice` is often the first step of modifying a hash into some other hash form, it makes sense to let it take an optional hash argument. 

 ``` 
 hash.slice(:created, :author, transform_keys: {created: :created_at}) 
 # => {created_at: 2019-10-23 17:54:46 +0900, author: "foo"} 
 ``` 

Back