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=-4.1 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, 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 9F4551F770 for ; Tue, 1 Jan 2019 23:14:30 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 18263114C48; Wed, 2 Jan 2019 08:14: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 9C2E61214A7 for ; Wed, 2 Jan 2019 08:14:23 +0900 (JST) Received: by filter0056p3mdw1.sendgrid.net with SMTP id filter0056p3mdw1-22663-5C2BF44B-49 2019-01-01 23:14:19.990650964 +0000 UTC m=+1040587.668644120 Received: from herokuapp.com (ec2-54-226-170-86.compute-1.amazonaws.com [54.226.170.86]) by ismtpd0066p1mdw1.sendgrid.net (SG) with ESMTP id -FXEtjQLR0-DOMqQ_-YCDQ for ; Tue, 01 Jan 2019 23:14:19.886 +0000 (UTC) Date: Tue, 01 Jan 2019 23:14:21 +0000 (UTC) From: tyler@tylerrick.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 66269 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15492 X-Redmine-Issue-Author: TylerRick X-Redmine-Sender: TylerRick 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/Ymy4QrNMhiuLXJG8OTL2vJD1yS4E/Mz3Sc1aMW9bgMWtodQwcBH6geOAoG5dih /p1r+FFhnNNMGsHmGiC/Vp8I1sTy9w8SiNrQJooOWmE5jbPIcMpkDE0CPZBYfSRGSfAE3f6Jfaksxv 7UWHJ6WdzgDK9iC+CdJSsjNz+vtwPQF9jAIaKwb5Yo/jWVKHZb4JyL+d9Q== X-ML-Name: ruby-core X-Mail-Count: 90847 Subject: [ruby-core:90847] [Ruby trunk Feature#15492] Let #dig take a "default value" block like Hash#fetch does 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 #15492 has been reported by TylerRick (Tyler Rick). ---------------------------------------- Feature #15492: Let #dig take a "default value" block like Hash#fetch does https://bugs.ruby-lang.org/issues/15492 * Author: TylerRick (Tyler Rick) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- [fetch](https://ruby-doc.org/core-2.6/Hash.html#method-i-fetch) provides multiple ways to handle the case where a key can't be found: > If the key can't be found, there are several options: With no other arguments, it will raise a KeyError exception; if `default` is given, then that will be returned; if the optional code block is specified, then that will be run and its result returned. ~~~ ruby {a: 'a'}.fetch(:d) { 'default' } #=> "default" {a: 'a'}.fetch(:d) {|key| key } #=> :d ~~~ [dig](https://ruby-doc.org/core-2.6/Hash.html#method-i-dig) obviously can't let you specify a `default` value as a positional argument like `fetch` does, but couldn't it at least let you specify a default value by passing a block? The fact that it currently just silently *ignores* the block that you pass to `dig` could be misleading, as one might assume (as I did at first) that it's going to return that in case any of the key lookups fail. Current/desired behavior: ~~~ ruby {a: {b: {c: 'c'}}}.dig(:a, :b, :d) {|key| key } #=> nil # wish it returned :d {a: {b: {c: 'c'}}}.dig(:a, :b, :d) {|key| 'default' } #=> nil # wish it returned 'default' ~~~ There isn't currently a nice way to do this (that I can think of). You could mix `dig` and `fetch` (or chain a bunch of `fetch`s), but that loses the simple elegance of `dig` and looks awful: ~~~ ruby object = {a: {b: {c: 'c'}}} (object.dig(:a, :b) || {}).fetch(:d) { 'default' } #=> "default" ~~~ Note, of course, that if we added default-block behavior to `dig`, its behavior would be slightly different: it would return the result of the default block if _any_ intermediate step were `nil`, not just if the _last_ lookup were nil. ~~~ ruby object = {a: {b: {c: 'c'}}} object.dig(:x, :y, :z) { 'default' } #=> "default" ~~~ ## Example use case Sometimes you start out with a simple Hash but over time you may end up moving keys into sub-hashes. You might have started out with something like ~~~ ruby config.fetch(:something) { 'default' } ~~~ But after you move `:something` under `:settings`, you have to use `dig` instead of `fetch`: ~~~ ruby config.dig(:settings, :something) ~~~ The problem is, how do you keep the default value now that it's in a sub-hash? Can't just use `|| 'default'` because the stored value might be `false`. Currently I'm working around this with an explicit `nil?` check... ~~~ ruby value = config.dig(:settings, :something) value.nil? ? 'default' : value ~~~ (Even *that* may not be good enough if one wanted to distinguish between a `nil` value _stored_ in the object and the case where the key can't be found (a "cache miss"). Having a default block would let you distinguish between a missing key and a nil value stored at the key, in case that distinction were important...) -- https://bugs.ruby-lang.org/