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.8 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_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 9FC7520248 for ; Tue, 16 Apr 2019 15:01:38 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 422A5120931; Wed, 17 Apr 2019 00:01:27 +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 6C2B2120915 for ; Wed, 17 Apr 2019 00:01:24 +0900 (JST) Received: by filter0021p3iad2.sendgrid.net with SMTP id filter0021p3iad2-15359-5CB5EE24-66 2019-04-16 15:00:52.632833223 +0000 UTC m=+75495.182508076 Received: from herokuapp.com (unknown [100.26.191.192]) by ismtpd0013p1iad2.sendgrid.net (SG) with ESMTP id HrAYv0yFRauVBGFoRPBjNQ for ; Tue, 16 Apr 2019 15:00:52.569 +0000 (UTC) Date: Tue, 16 Apr 2019 15:01:14 +0000 (UTC) From: manga.osyo@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 67788 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 13645 X-Redmine-Issue-Author: ndn X-Redmine-Sender: osyo 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?EoIqruA4Er0CjiyK1+U6TJuuQMCAZ3OX2ybarlx6ke4ErSuFz8teGaYBBPEht6?= =?us-ascii?Q?dxViQKSg5N2dwGyebQKhPanFNtXbyyGvLxQUQE9?= =?us-ascii?Q?uz0YS2kEk=2Fo3HY5RhTBwV7CPmXudE+j2rKCO2Wg?= =?us-ascii?Q?U4k7zxfPyytPPaLrcGo7=2FwkvYrXqEeiOdicrN5Y?= =?us-ascii?Q?Ps64rs3h=2FrI3dKHL67DfWlbok+3z+A9aYYQ=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 92305 Subject: [ruby-core:92305] [Ruby trunk Feature#13645] Syntactic sugar for indexing when using the safe navigation operator 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 #13645 has been updated by osyo (manga osyo). hi. I would like to use `hash&.[key]` (or `hash&[key]` ) in following cases as below. ```ruby class X def initialize @hash = { a: 1, b: 2, c: 3 } end def [](key) @hash[key] end end def get(key) x = Symbol === key && X.new || nil # I want to use x&.[key] (or x&[key]) x&.[](key) end p get(:a) #=> 1 p get("b") # => nil ``` Matz writes > Use #dig for referencing the value. > For updating, show us use cases. > https://bugs.ruby-lang.org/issues/11813#note-6 However, YOU can not use `x&.dig(key)` for a class that Mr. Matz has previously proposed for which #dig is not defined. ---------------------------------------- Feature #13645: Syntactic sugar for indexing when using the safe navigation operator https://bugs.ruby-lang.org/issues/13645#change-77648 * Author: ndn (Nikola Nenkov) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- # Proposal While it works and makes sense, this is a bit cumbersome: ```ruby hash&.[](:key) ``` Ideally, we could use something like: ```ruby hash&.[:key] ``` -- https://bugs.ruby-lang.org/