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=-3.9 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham 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 70C5A1F463 for ; Mon, 23 Dec 2019 04:53:36 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 01C1F1209C5; Mon, 23 Dec 2019 13:53:21 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id CA1061209C4 for ; Mon, 23 Dec 2019 13:53:20 +0900 (JST) Received: by filterdrecv-p3mdw1-56c97568b5-bbmbb with SMTP id filterdrecv-p3mdw1-56c97568b5-bbmbb-18-5E004845-E 2019-12-23 04:53:25.309493798 +0000 UTC m=+533413.946064770 Received: from herokuapp.com (unknown [18.233.7.172]) by ismtpd0006p1iad2.sendgrid.net (SG) with ESMTP id Wn0AmlarSE-hfmsu-Zt7Kw for ; Mon, 23 Dec 2019 04:53:25.202 +0000 (UTC) Date: Mon, 23 Dec 2019 04:53:25 +0000 (UTC) From: duerst@it.aoyama.ac.jp Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 72075 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16274 X-Redmine-Issue-Author: sawa X-Redmine-Sender: duerst 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?uQY=2F2xNrNfHHTWbKn6MBvvzfU5Pqk9I4lnOVb0CFDut+6yhHI+pjDK3ZqnpnLj?= =?us-ascii?Q?H8dN3jduMBJcvRb8+BfcMQ=2FK2BIRBqIFjx=2F8nwC?= =?us-ascii?Q?bdzYEqKSFL4ZZIj13VxbqrtdB46QYVBQVvtGkzF?= =?us-ascii?Q?yumm5uMW8EHp=2Fp7W65XvTQVFTCTgNFJV4yUsQ+D?= =?us-ascii?Q?hrWomK68MgDF6D=2FZ5TMxUoHvfFrkY6tUtq2B2UU?= =?us-ascii?Q?LsKa47nZq88FAIwvI=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 96416 Subject: [ruby-core:96416] [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="iso-8859-1" Content-Transfer-Encoding: quoted-printable Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #16274 has been updated by duerst (Martin D=FCrst). 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 d= o 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 fro= m the old keys to the new keys follows some rule. But in reality, we freque= ntly want to change the keys where it is difficult to provide a rule. For e= xample, suppose we have: ``` hash =3D {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 +0= 900, 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}) # =3D> {created_at: 2019-10-23 17:54:46 +0900, update_time: 2019-10-23 17:5= 9: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 ot= her hash form, it makes sense to let it take an optional hash argument. ``` hash.slice(:created, :author, transform_keys: {created: :created_at}) # =3D> {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 bloc= k simultaneously: ``` hash.transform_keys({created: :created_at, updated: :update_time}, &:to_s) # =3D> {"created_at" =3D> 2019-10-23 17:54:46 +0900, "update_time" =3D> 201= 9-10-23 17:59:18 +0900, "author" =3D> "foo"} ``` -- = https://bugs.ruby-lang.org/ Unsubscribe: