From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: poffice@blade.nagaokaut.ac.jp Delivered-To: poffice@blade.nagaokaut.ac.jp Received: from kankan.nagaokaut.ac.jp (kankan.nagaokaut.ac.jp [133.44.2.24]) by blade.nagaokaut.ac.jp (Postfix) with ESMTP id 5157319C0036 for ; Wed, 4 Nov 2015 08:35:09 +0900 (JST) Received: from voscc.nagaokaut.ac.jp (voscc.nagaokaut.ac.jp [133.44.1.100]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id 681EDB5DA1D for ; Wed, 4 Nov 2015 09:03:55 +0900 (JST) Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by voscc.nagaokaut.ac.jp (Postfix) with ESMTP id 4CED4952443 for ; Wed, 4 Nov 2015 09:03:55 +0900 (JST) Received: from [221.186.184.76] (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id EB1B31204DF; Wed, 4 Nov 2015 09:03:53 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from o2.heroku.sendgrid.net (o2.heroku.sendgrid.net [67.228.50.55]) by neon.ruby-lang.org (Postfix) with ESMTPS id 21FF01204B9 for ; Wed, 4 Nov 2015 09:03:49 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sendgrid.me; h=from:to:references:subject:mime-version:content-type:content-transfer-encoding:list-id; s=smtpapi; bh=oXktRcbsC8teL3a7f28CG6XYm2w=; b=qTBdsQwBHCDaBcPY/j XXueiHyI0w31Yn6gsC0JakyYd7ZUMECzUBrmIH2VEQafHxSSuceng5X8oR1HLkmo x47y4j43yBoby7vE3uU2e0TEdI5RvGlaF4MpRb2bGqhHjwJmhy01ZgH90zh5uz9X q8qtktwnevklyYo6LlhOiGnzc= Received: by filter0428p1mdw1.sendgrid.net with SMTP id filter0428p1mdw1.22854.56394B5D3E 2015-11-04 00:03:41.682888387 +0000 UTC Received: from herokuapp.com (ec2-54-163-168-234.compute-1.amazonaws.com [54.163.168.234]) by ismtpd0001p1iad1.sendgrid.net (SG) with ESMTP id 3M2Nb4IQRvWqAXjDdV_l8A for ; Wed, 04 Nov 2015 00:03:41.669 +0000 (UTC) Date: Wed, 04 Nov 2015 00:03:41 +0000 From: dsisnero@gmail.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Redmine-MailingListIntegration-Message-Ids: 45963 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 11643 X-Redmine-Issue-Author: gkop X-Redmine-Sender: dsisnero 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/Ymy4QrNMhiuLXJG8OTL2vJD1yS6Ll+HccOI0RqU7F5Wu4lqzq1/tqwxmXMKBNe lsNUyzFDSFug2QIoAzAMMQgmUszUi7OWJZwJNFDsZLB/Wh7A73WBccXy6QjfdKH9UC5iCzEKiQg+yU X7vJqXKRql4ly8QtPOg+VawFPE2EAKgagxcw X-ML-Name: ruby-core X-Mail-Count: 71323 Subject: [ruby-core:71323] [Ruby trunk - Feature #11643] A new method on Hash to grab values out of nested hashes, failing gracefully 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: , Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #11643 has been updated by Dominic Sisneros. Yukihiro Matsumoto wrote: > I prefer method way to (already reverted) `params.?[:order].?[:shipping_info].?[:country]`. > I am not sure `dig` is the best name for it. It's short, concise thought. > Any other idea, anyone? > > Matz. clojure has get-in for their maps, how about fetch_in with replacement like fetch hash.fetch_in(:order, :shipping_info, :country, 'Not found') ---------------------------------------- Feature #11643: A new method on Hash to grab values out of nested hashes, failing gracefully https://bugs.ruby-lang.org/issues/11643#change-54698 * Author: Gabe Kopley * Status: Open * Priority: Normal * Assignee: ---------------------------------------- (I posted this to the mailing list last year [0] and received no response, but am inspired to file an issue here based on the positive reception to https://bugs.ruby-lang.org/issues/11537 ) This comes up sometimes in Rails programming [1]: `params[:order] && params[:order][:shipping_info] && params[:order][:shipping_info][:country]` or `params[:order][:shipping_info][:country] rescue nil` or `params.fetch(:order, {}).fetch(:shipping_info, {}).fetch(:country, nil)` What if Hash gave us a method to accomplish this more concisely and semantically? Eg. `params.traverse_nested_hashes_and_return_nil_if_a_key_isnt_found(:order, :shipping_info, :country)` Or to take a nice method name suggestion [2]: `params.dig(:order, :shipping_info, :country)` Another example solution is https://github.com/intridea/hashie#deepfetch (Although I don't like "fetch" in this method name since it doesn't and can't take a default value as an argument like Hash#fetch does) What do you all think? [0] https://groups.google.com/forum/#!topic/ruby-core-google/guleNgEJWcM [1] https://groups.google.com/d/msg/rubyonrails-core/bOkvcvS3t_A/QXLEXwt9ivAJ https://stackoverflow.com/questions/1820451/ruby-style-how-to-check-whether-a-nested-hash-element-exists https://stackoverflow.com/questions/19115838/how-do-i-use-the-fetch-method-for-nested-hash https://stackoverflow.com/questions/10130726/ruby-access-multidimensional-hash-and-avoid-access-nil-object [2] http://stackoverflow.com/a/1820492/283398 -- https://bugs.ruby-lang.org/