ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: matz@ruby.or.jp
To: ruby-core@ruby-lang.org
Subject: [ruby-core:96370] [Ruby master Feature#16274] Transform hash keys by a hash
Date: Fri, 20 Dec 2019 07:31:27 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-83284.20191220073127.117c0cf3d71f2da6@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16274.20191023090915@ruby-lang.org

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/

  parent reply	other threads:[~2019-12-20  7:31 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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 ` matz [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-83284.20191220073127.117c0cf3d71f2da6@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).