ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:95496] [Ruby master Feature#16274] Transform hash keys by a hash
       [not found] <redmine.issue-16274.20191023090915@ruby-lang.org>
@ 2019-10-23  9:09 ` sawadatsuyoshi
  2019-10-23  9:16 ` [ruby-core:95497] " shyouhei
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: sawadatsuyoshi @ 2019-10-23  9:09 UTC (permalink / raw)
  To: ruby-core

Issue #16274 has been reported by sawa (Tsuyoshi Sawada).

----------------------------------------
Feature #16274: Transform hash keys by a hash
https://bugs.ruby-lang.org/issues/16274

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
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 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"}
```




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:95497] [Ruby master Feature#16274] Transform hash keys by a hash
       [not found] <redmine.issue-16274.20191023090915@ruby-lang.org>
  2019-10-23  9:09 ` [ruby-core:95496] [Ruby master Feature#16274] Transform hash keys by a hash sawadatsuyoshi
@ 2019-10-23  9:16 ` shyouhei
  2019-10-23  9:20 ` [ruby-core:95498] " sawadatsuyoshi
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: shyouhei @ 2019-10-23  9:16 UTC (permalink / raw)
  To: ruby-core

Issue #16274 has been updated by shyouhei (Shyouhei Urabe).


Understand the motivation (maybe that of #slice can be separated into another request).

One quick question: what should happen if _both_ a block and an argument are passed at once?

----------------------------------------
Feature #16274: Transform hash keys by a hash
https://bugs.ruby-lang.org/issues/16274#change-82271

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
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_keys({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"}
```




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:95498] [Ruby master Feature#16274] Transform hash keys by a hash
       [not found] <redmine.issue-16274.20191023090915@ruby-lang.org>
  2019-10-23  9:09 ` [ruby-core:95496] [Ruby master Feature#16274] Transform hash keys by a hash sawadatsuyoshi
  2019-10-23  9:16 ` [ruby-core:95497] " shyouhei
@ 2019-10-23  9:20 ` sawadatsuyoshi
  2019-10-23  9:37 ` [ruby-core:95499] " duerst
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: sawadatsuyoshi @ 2019-10-23  9:20 UTC (permalink / raw)
  To: ruby-core

Issue #16274 has been updated by sawa (Tsuyoshi Sawada).


shyouhei (Shyouhei Urabe) wrote:
> Understand the motivation (maybe that of #slice can be separated into another request).
> 
> One quick question: what should happen if _both_ a block and an argument are passed at once?

Actually, I just came up with an additional idea regarding that exact point. Updated the issue.

----------------------------------------
Feature #16274: Transform hash keys by a hash
https://bugs.ruby-lang.org/issues/16274#change-82273

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
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_keys({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"}
```


With option 1, it could make sense to even allow a hash argument and a block simultaneously:

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




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:95499] [Ruby master Feature#16274] Transform hash keys by a hash
       [not found] <redmine.issue-16274.20191023090915@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2019-10-23  9:20 ` [ruby-core:95498] " sawadatsuyoshi
@ 2019-10-23  9:37 ` duerst
  2019-10-23 19:46 ` [ruby-core:95513] " shevegen
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: duerst @ 2019-10-23  9:37 UTC (permalink / raw)
  To: ruby-core

Issue #16274 has been updated by duerst (Martin Dürst).


`String#gsub` also can take a block or a hash. Using a hash for `String#gsub` isn't possible in Perl or Python, but can be extremely handy. Maybe the behavior when there's both a block and a hash could be the same as for `String#gsub`?

----------------------------------------
Feature #16274: Transform hash keys by a hash
https://bugs.ruby-lang.org/issues/16274#change-82275

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
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 the old keys to the new keys follows 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_keys({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"}
```


With option 1, it could make sense to even allow a hash argument and a block simultaneously:

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




-- 
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>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:95513] [Ruby master Feature#16274] Transform hash keys by a hash
       [not found] <redmine.issue-16274.20191023090915@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2019-10-23  9:37 ` [ruby-core:95499] " duerst
@ 2019-10-23 19:46 ` shevegen
  2019-12-20  7:28 ` [ruby-core:96369] " knu
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: shevegen @ 2019-10-23 19:46 UTC (permalink / raw)
  To: ruby-core

Issue #16274 has been updated by shevegen (Robert A. Heiler).


Personally I have had a need to transform keys (and values) in a hash quite a bit. We
also have strange thingies such as HashWithIndifferentAccess so that may be indicative
of people wondering about strings/symbols as keys for a hash in general. :)

To sawa's suggestion: if the choice is solely between option 1 and option 2,
I personally would favour 1, mostly because I only rarely use slice in 
general (primarily I may use slice in regards to String, but even then I tend
to prefer e. g. [] start, end positions normally, even if it may not be the
same, such as if we include .slice!() use cases). But this may differ on
an individual choice, by different ruby users, so I can not generalize it;
it is only an opinion if the choice is purely between #1 and #2 alone.

I do not know how frequent the use case may be, but ignoring this for the
moment I think this suggestion may be useful.

----------------------------------------
Feature #16274: Transform hash keys by a hash
https://bugs.ruby-lang.org/issues/16274#change-82289

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
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 the old keys to the new keys follows 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_keys({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"}
```


With option 1, it could make sense to even allow a hash argument and a block simultaneously:

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




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:96369] [Ruby master Feature#16274] Transform hash keys by a hash
       [not found] <redmine.issue-16274.20191023090915@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2019-10-23 19:46 ` [ruby-core:95513] " shevegen
@ 2019-12-20  7:28 ` knu
  2019-12-20  7:31 ` [ruby-core:96370] " matz
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: knu @ 2019-12-20  7:28 UTC (permalink / raw)
  To: ruby-core

Issue #16274 has been updated by knu (Akinori MUSHA).


Which does `hash1.transform_keys(hash2)` iterate over, hash1's keys or  hash2's keys?

----------------------------------------
Feature #16274: Transform hash keys by a hash
https://bugs.ruby-lang.org/issues/16274#change-83283

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
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 the old keys to the new keys follows 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_keys({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"}
```


With option 1, it could make sense to even allow a hash argument and a block simultaneously:

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




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:96370] [Ruby master Feature#16274] Transform hash keys by a hash
       [not found] <redmine.issue-16274.20191023090915@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2019-12-20  7:28 ` [ruby-core:96369] " knu
@ 2019-12-20  7:31 ` matz
  2019-12-23  4:53 ` [ruby-core:96416] " duerst
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: matz @ 2019-12-20  7:31 UTC (permalink / raw)
  To: ruby-core

Issue #16274 has been updated by matz (Yukihiro Matsumoto).


I understand. Accepted.
FYI, we do not accept a similar change to `transform_values`.

Matz.


----------------------------------------
Feature #16274: Transform hash keys by a hash
https://bugs.ruby-lang.org/issues/16274#change-83284

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
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 the old keys to the new keys follows 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_keys({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"}
```


With option 1, it could make sense to even allow a hash argument and a block simultaneously:

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




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:96416] [Ruby master Feature#16274] Transform hash keys by a hash
       [not found] <redmine.issue-16274.20191023090915@ruby-lang.org>
                   ` (6 preceding siblings ...)
  2019-12-20  7:31 ` [ruby-core:96370] " matz
@ 2019-12-23  4:53 ` duerst
  2019-12-23  8:54 ` [ruby-core:96420] " sawadatsuyoshi
  2019-12-23  8:57 ` [ruby-core:96421] " sawadatsuyoshi
  9 siblings, 0 replies; 10+ messages in thread
From: duerst @ 2019-12-23  4:53 UTC (permalink / raw)
  To: ruby-core

Issue #16274 has been updated by duerst (Martin Dürst).


matz (Yukihiro Matsumoto) wrote:

> FYI, we do not accept a similar change to `transform_values`.

Matz - Do you mean "we reject a similar change to `transform_values`", or do you mean "we have not yet accepted a similar change to `transform_values` (but could consider it later)"?

----------------------------------------
Feature #16274: Transform hash keys by a hash
https://bugs.ruby-lang.org/issues/16274#change-83340

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 2.8
----------------------------------------
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 the old keys to the new keys follows 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_keys({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"}
```


With option 1, it could make sense to even allow a hash argument and a block simultaneously:

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




-- 
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>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:96420] [Ruby master Feature#16274] Transform hash keys by a hash
       [not found] <redmine.issue-16274.20191023090915@ruby-lang.org>
                   ` (7 preceding siblings ...)
  2019-12-23  4:53 ` [ruby-core:96416] " duerst
@ 2019-12-23  8:54 ` sawadatsuyoshi
  2019-12-23  8:57 ` [ruby-core:96421] " sawadatsuyoshi
  9 siblings, 0 replies; 10+ messages in thread
From: sawadatsuyoshi @ 2019-12-23  8:54 UTC (permalink / raw)
  To: ruby-core

Issue #16274 has been updated by sawa (Tsuyoshi Sawada).


As for the behavior when both a hash argument and a block are given, I suggested in the issue to have the block applied after the hash has applied:

**Original proposal**
```ruby
hash.transform_keys({created: :created_at, updated: :update_time}, &:to_s)
# => {"created_at" => ..., "update_time" => ..., "author" => ...}
```

But I reconsidered this, and now I think that the block should be applied to the residue of what the hash has applied. (In other words, the hash and the block should be applied mutually exclusively of each other, with the hash having priority over the block.) Hence, I expect the following results:

**New proposal**
```ruby
hash.transform_keys({created: :created_at, updated: :update_time}, &:to_s)
# => {:created_at => ..., :update_time => ..., "author" => ...}

hash.transform_keys({created: "created_at", updated: "update_time"}, &:to_s)
# => {"created_at" => ..., "update_time" => ..., "author" => ...}
```

The reason is twofold.

First, my original proposal would lead to redundancy; I would have to provide the intermediate key `:created_at`, `:update_time`, knowing that they will not appear in the final output because of the further transformation of them into strings due to the block. Providing the final keys `"created_at"` and `"update_time"` from the beginning would be more straightforward, and will save some internal calculations to be done by Ruby.

Second, the new proposal will have more expressive power. Suppose I actually wanted:

```ruby
{:created_at => ..., :update_time => ..., "author" => ...}
```

That can be done with the new proposal, but not with my original proposal.

----------------------------------------
Feature #16274: Transform hash keys by a hash
https://bugs.ruby-lang.org/issues/16274#change-83343

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 2.8
----------------------------------------
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 the old keys to the new keys follows 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_keys({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"}
```


With option 1, it could make sense to even allow a hash argument and a block simultaneously:

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




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [ruby-core:96421] [Ruby master Feature#16274] Transform hash keys by a hash
       [not found] <redmine.issue-16274.20191023090915@ruby-lang.org>
                   ` (8 preceding siblings ...)
  2019-12-23  8:54 ` [ruby-core:96420] " sawadatsuyoshi
@ 2019-12-23  8:57 ` sawadatsuyoshi
  9 siblings, 0 replies; 10+ messages in thread
From: sawadatsuyoshi @ 2019-12-23  8:57 UTC (permalink / raw)
  To: ruby-core

Issue #16274 has been updated by sawa (Tsuyoshi Sawada).


knu (Akinori MUSHA) wrote:
> Which does `hash1.transform_keys(hash2)` iterate over, hash1's keys or  hash2's keys?

My intention of including the key `:author` in `hash` was to show that the iteration is done over the receiver (your `hash1`), not the hash argument (your `hash2`).

----------------------------------------
Feature #16274: Transform hash keys by a hash
https://bugs.ruby-lang.org/issues/16274#change-83344

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 2.8
----------------------------------------
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 the old keys to the new keys follows 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_keys({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"}
```


With option 1, it could make sense to even allow a hash argument and a block simultaneously:

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




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2019-12-23  8:57 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-16274.20191023090915@ruby-lang.org>
2019-10-23  9:09 ` [ruby-core:95496] [Ruby master Feature#16274] Transform hash keys by a hash sawadatsuyoshi
2019-10-23  9:16 ` [ruby-core:95497] " shyouhei
2019-10-23  9:20 ` [ruby-core:95498] " sawadatsuyoshi
2019-10-23  9:37 ` [ruby-core:95499] " duerst
2019-10-23 19:46 ` [ruby-core:95513] " shevegen
2019-12-20  7:28 ` [ruby-core:96369] " knu
2019-12-20  7:31 ` [ruby-core:96370] " matz
2019-12-23  4:53 ` [ruby-core:96416] " duerst
2019-12-23  8:54 ` [ruby-core:96420] " sawadatsuyoshi
2019-12-23  8:57 ` [ruby-core:96421] " sawadatsuyoshi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).