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.2 required=3.0 tests=BAYES_00,FORGED_HOTMAIL_RCVD2, 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 CA2DD1F453 for ; Mon, 28 Jan 2019 17:06:11 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 003191218E1; Tue, 29 Jan 2019 02:06:08 +0900 (JST) Received: from o1678916x28.outbound-mail.sendgrid.net (o1678916x28.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 772381218E1 for ; Tue, 29 Jan 2019 02:06:07 +0900 (JST) Received: by filter0048p3mdw1.sendgrid.net with SMTP id filter0048p3mdw1-15425-5C4F367B-4C 2019-01-28 17:06:03.895699277 +0000 UTC m=+404581.507894134 Received: from herokuapp.com (ec2-54-226-102-166.compute-1.amazonaws.com [54.226.102.166]) by ismtpd0042p1mdw1.sendgrid.net (SG) with ESMTP id l1Q5hdNrTtuB5eqPvrm7fQ for ; Mon, 28 Jan 2019 17:06:03.888 +0000 (UTC) Date: Mon, 28 Jan 2019 17:06:05 +0000 (UTC) From: djo.went@hotmail.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 66759 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15563 X-Redmine-Issue-Author: 3limin4t0r X-Redmine-Sender: 3limin4t0r 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: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS5jOGwvvaS+JCA0pFUWaaSgMgkTmP2kAzqAIC lRm8EzUaA6eaSa1Z43uU24wT+aLocaC+2lCSZ7rvymzffOlViFHPd8JOEnqxNFG8uWmxitfuQFIaxM uwkt+Fj4Iyr5VQMyLp3GfPZnX0yOtgrmtZzIEtAnqQOKURVzxhjiNA4q8Q== X-ML-Name: ruby-core X-Mail-Count: 91308 Subject: [ruby-core:91308] [Ruby trunk Feature#15563] #dig that throws an exception if an key doesn't exist 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 #15563 has been updated by 3limin4t0r (Johan Wentholt). My scenario would be the similar as described by k0kubun. ```Ruby # The connection translates the request to JSON and parses the response # from JSON into the correct objects. In this case a nested hash structure. response = connection.send(request) # assign shortcuts report = response .fetch('Era.Common.NetworkMessage.ConsoleApi.Reports.RpcGenerateReportResponse') .fetch('report') column_data = report.fetch('data').fetch('columns') column_labels = report.fetch('rendering').fetch('table').fetch('columns') # build report report_data = column_data.each_with_object({}) do |column, data| column_id = column.fetch('header').fetch('column_id') data[column_id] = column.fetch('values') end report = column_labels.each_with_object({}) do |column, data| label = column.fetch('label').fetch('literal') column_id = column.fetch('column_id') data[label] = report_data.fetch(column_id) end ``` >From the above scenario you can see that having this new functionality would help out a lot. The reason I use `#fetch` here is because the API to which I'm talking might change it's structure. Getting an error as soon as possible reduces debug time, since the code won't move on with a `nil` value (that probably raises an exception somewhere later). ---------------------------------------- Feature #15563: #dig that throws an exception if an key doesn't exist https://bugs.ruby-lang.org/issues/15563#change-76554 * Author: 3limin4t0r (Johan Wentholt) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- Ruby 2.3.0 introduced `#dig` for *Array*, *Hash* and *Struct*. Both *Array* and *Hash* have `#fetch` which does the same as `#[]`, but instead of returning the default value an exception is raised (unless a second argument or block is given). *Hash* also has `#fetch_values` which does the same as `#values_at`, raising an exception if an key is missing. For `#dig` there is no such option. My proposal is to add a method which does the same as `#dig`, but instead of using the `#[]` accessor it uses `#fetch`. This method would look something like this: ```Ruby module DigWithException def dig_e(key, *others) value = fetch(key) return value if value.nil? || others.empty? if value.respond_to?(__method__, true) value.send(__method__, *others) else raise TypeError, "#{value.class} does not have ##{__method__} method" end end end Array.include(DigWithException) Hash.include(DigWithException) ``` The exception raised is also taken from `#dig` (`[1].dig(0, 1) #=> TypeError: Integer does not have #dig method`). I personally have my issues with the name `#dig_e`, but I haven't found a better name yet. There are also a few other things that I haven't thought out yet. 1. Should this method be able to accept a block which, will be passed to the `#fetch` call and recursive `#dig_e` calls? ```Ruby module DigWithException def dig_e(key, *others, &block) value = fetch(key, &block) return value if value.nil? || others.empty? if value.respond_to?(__method__, true) value.send(__method__, *others, &block) else raise TypeError, "#{value.class} does not have ##{__method__} method" end end end Array.include(DigWithException) Hash.include(DigWithException) ``` 2. I currently kept the code compatible with the `#dig` description. > Extracts the nested value specified by the sequence of *key* objects by calling `dig` at each step, returning `nil` if any intermediate step is `nil`. However, with this new version of the method one could consider dropping the *"returning `nil` if any intermediate step is `nil`"* part, since this would be the more strict version. ```Ruby module DigWithException def dig_e(key, *others) value = fetch(key) return value if others.empty? if value.respond_to?(__method__, true) value.send(__method__, *others) else raise TypeError, "#{value.class} does not have ##{__method__} method" end end end Array.include(DigWithException) Hash.include(DigWithException) ``` I'm curious to hear what you guys think about the idea as a whole, the method name and the two points described above. -- https://bugs.ruby-lang.org/