From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-2.6 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=no autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 3110E1F463 for ; Mon, 23 Dec 2019 08:54:33 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id E45F6120A12; Mon, 23 Dec 2019 17:54:17 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 71B88120915 for ; Mon, 23 Dec 2019 17:54:15 +0900 (JST) Received: by filterdrecv-p3mdw1-56c97568b5-qqd9g with SMTP id filterdrecv-p3mdw1-56c97568b5-qqd9g-18-5E0080BA-25 2019-12-23 08:54:18.645905796 +0000 UTC m=+547869.296457646 Received: from herokuapp.com (unknown [18.233.7.172]) by ismtpd0035p1mdw1.sendgrid.net (SG) with ESMTP id 7dtIQmZfQjCjBfNEExmIQw for ; Mon, 23 Dec 2019 08:54:18.589 +0000 (UTC) Date: Mon, 23 Dec 2019 08:54:18 +0000 (UTC) From: sawadatsuyoshi@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 72079 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16274 X-Redmine-Issue-Author: sawa X-Redmine-Sender: sawa X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?jFXA8Rt481sXUUIO9tYW1AJlMOZdNdlSw=2F5TfLCefGuaXX9MMnjW8hcITnR=2FrK?= =?us-ascii?Q?le6HJ9pTnY4m6HYOE1iCRJzYha4gozgdQ6XhHvb?= =?us-ascii?Q?TPe0w3G8=2FWt4AVLHBX2lh7=2FmtmEnIsvutGFDOjZ?= =?us-ascii?Q?t7xXjDtGHvAn+uZcV1hVaF9nVABwTlFdRXqSW17?= =?us-ascii?Q?+JimmvBRnHKRZubCTkGfgh2eqXUayWztJQOg0Td?= =?us-ascii?Q?6CAD5wr598bObfB20=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 96420 Subject: [ruby-core:96420] [Ruby master Feature#16274] Transform hash keys by a hash X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "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/