Project

General

Profile

Actions

Feature #16274

closed

Transform hash keys by a hash

Added by sawa (Tsuyoshi Sawada) over 4 years ago. Updated over 3 years ago.

Status:
Closed
Assignee:
-
Target version:
[ruby-core:<unknown>]

Description

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"}
Actions #1

Updated by sawa (Tsuyoshi Sawada) over 4 years ago

  • Description updated (diff)
Actions #2

Updated by sawa (Tsuyoshi Sawada) over 4 years ago

  • Description updated (diff)

Updated by shyouhei (Shyouhei Urabe) over 4 years ago

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?

Actions #4

Updated by sawa (Tsuyoshi Sawada) over 4 years ago

  • Description updated (diff)

Updated by sawa (Tsuyoshi Sawada) over 4 years ago

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.

Actions #6

Updated by sawa (Tsuyoshi Sawada) over 4 years ago

  • Description updated (diff)

Updated by duerst (Martin Dürst) over 4 years ago

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?

Updated by shevegen (Robert A. Heiler) over 4 years ago

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.

Updated by knu (Akinori MUSHA) over 4 years ago

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

Updated by matz (Yukihiro Matsumoto) over 4 years ago

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

Matz.

Actions #11

Updated by nobu (Nobuyoshi Nakada) over 4 years ago

  • Target version set to 36

Updated by duerst (Martin Dürst) over 4 years ago

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)"?

Updated by sawa (Tsuyoshi Sawada) over 4 years ago

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

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

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:

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

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

Updated by sawa (Tsuyoshi Sawada) over 4 years ago

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

Actions #15

Updated by znz (Kazuhiro NISHIYAMA) about 4 years ago

  • Status changed from Open to Closed
Actions #16

Updated by hsbt (Hiroshi SHIBATA) over 3 years ago

  • Target version changed from 36 to 3.0

Updated by marcandre (Marc-Andre Lafortune) over 3 years ago

Closing, since this has been committed by Nobu 🎉 (in b25e27277dc39)

I just added doc (in 8558d5e48)

Updated by solnic (Peter Solnica) over 3 years ago

marcandre (Marc-Andre Lafortune) wrote in #note-17:

Closing, since this has been committed by Nobu 🎉 (in b25e27277dc39)

I just added doc (in 8558d5e48)

This is a wonderful addition but is there a chance you'd reconsider it? Instead of making transform_keys more complex, wouldn't it be better to introduce Hash#rename_keys instead? It could be even built on top of Hash#transform_keys.

Updated by marcandre (Marc-Andre Lafortune) over 3 years ago

solnic (Piotr Solnica) wrote in #note-18:

marcandre (Marc-Andre Lafortune) wrote in #note-17:

Closing, since this has been committed by Nobu 🎉 (in b25e27277dc39)

I just added doc (in 8558d5e48)

This is a wonderful addition but is there a chance you'd reconsider it? Instead of making transform_keys more complex, wouldn't it be better to introduce Hash#rename_keys instead? It could be even built on top of Hash#transform_keys.

It looks like you are replying to me, but of course this is Matz' decision. That said I feel that the current name is good, especially since you can mix and match hash-mapping and block.

Updated by mame (Yusuke Endoh) over 3 years ago

Looks like everyone has forgotten Hash#to_proc?

irb(main):001:0> { a: 1 }.transform_keys({ a: :b })
=> {:b=>1}
irb(main):002:0> { a: 1 }.transform_keys(&{ a: :b })
=> {:b=>1}
Actions #21

Updated by mame (Yusuke Endoh) over 3 years ago

Ah, I overlooked.

irb(main):003:0> { a: 1, c: 1 }.transform_keys({ a: :b })
=> {:b=>1, :c=>1}
irb(main):004:0> { a: 1, c: 1 }.transform_keys(&{ a: :b })
=> {:b=>1, nil=>1}
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0