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 AE86419C003E for ; Sun, 8 Nov 2015 04:24:44 +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 DB7FDB5D870 for ; Sun, 8 Nov 2015 04:54:00 +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 E013B18CC7B1 for ; Sun, 8 Nov 2015 04:54:00 +0900 (JST) Received: from [221.186.184.76] (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id E5607120494; Sun, 8 Nov 2015 04:53:58 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from o10.shared.sendgrid.net (o10.shared.sendgrid.net [173.193.132.135]) by neon.ruby-lang.org (Postfix) with ESMTPS id 3A8D112045B for ; Sun, 8 Nov 2015 04:53:54 +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=GFvK8UAv287q2oTYY0/Azwp4V4g=; b=wYcih2O2AskuNVWGQK rXPGf1CSV4hShVXcpwHYUDZE3l+h2onPINF0lB+5ytRz0ZJCzVOKgWX/u8CIkpC2 eE5FrGUt4DalU9T+wXVgUG6Sah2cb/OMINMb/lLCTlfvi5g3p7QRWauJjN6JNsWX HF4Wjcp8d3Ld121pAKczvP/0M= Received: by filter-476.sjc1.sendgrid.net with SMTP id filter-476.27090.563E56CF26 2015-11-07 19:53:51.595574848 +0000 UTC Received: from herokuapp.com (ec2-23-22-28-195.compute-1.amazonaws.com [23.22.28.195]) by ismtpd0004p1iad1.sendgrid.net (SG) with ESMTP id NIIIfNiMRM64Arkj6vjO0Q for ; Sat, 07 Nov 2015 19:53:51.543 +0000 (UTC) Date: Sat, 07 Nov 2015 19:53:51 +0000 From: keithrbennett@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: quoted-printable X-Redmine-MailingListIntegration-Message-Ids: 46022 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 11643 X-Redmine-Issue-Author: gkop X-Redmine-Sender: keithrbennett 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/Ymy4QrNMhiuLXJG8OTL2vJD1yS7RSUiUmSwPb3OSxSxU/uPxGmiC3NfxrkwuG+ EwmlL+3ZoWiufmoafde1Get58Eu4zMUpsJ9BPRUIVIUjZMuA7VEVBNq48wKltsk3dvdrUfZeVRnw7V S+1u5KpOq5H2TL4= X-ML-Name: ruby-core X-Mail-Count: 71380 Subject: [ruby-core:71380] [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 Keith Bennett. I like the 'dig' method approach for these reasons: * it does not require any fanciness or magic that could confuse novice Ruby= ists * it does not require any change to the interpreter * the name 'dig' is concise and intention-revealing I have been hoping for this feature for a long time. This would be great. Austin Ziegler wrote: > The problem with `hash.fetch_in(:order, :shipping_info, :country, 'Not > found')` is that `'Not found'` is a (possibly) valid key. You would need= to > implement this with a kwarg. >=20=20 > ```ruby > class Hash > def fetch_in(*keys, **kwargs, &block) > keys =3D keys.dup > ckey =3D keys.shift >=20=20 > unless self.key?(ckey) > return kwargs[:default] if kwargs.key?(:default) > return block.call(ckey) if block > fail KeyError, "key not found #{ckey.inspect}" > end >=20=20 > child =3D self[ckey] >=20=20 > if keys.empty? > child > elsif child.respond_to?(:fetch_in) > child.fetch_in(*keys, **kwargs, &block) > else > fail ArgumentError, 'more keys than Hashes' > end > end > end >=20=20 > a =3D { > a: { > b: { > c: :d > } > } > } >=20=20 > def y > yield > rescue =3D> e > e > end >=20=20 > p y { a } > p y { a.fetch_in(:a) } > p y { a.fetch_in(:a, :b) } > p y { a.fetch_in(:a, :b, :c) } > p y { a.fetch_in(:a, :b, :c, :d) } > p y { a.fetch_in(:a, :b, :d) } > p y { a.fetch_in(:a, :b, :d, default: 'z') } > p y { a.fetch_in(:a, :b, :d) { 'z' } } > ``` >=20=20 > As a proposed name, I suggest `locate`. >=20=20 > On Tue, Nov 3, 2015 at 7:03 PM, wrote: >=20=20 > > 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 tho= ught. > > > 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 hash= es, > > 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 respo= nse, > > 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, ni= l)` > > > > 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(:ord= er, > > :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#deepfet= ch > > (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/guleNgEJW= cM > > > > [1] > > https://groups.google.com/d/msg/rubyonrails-core/bOkvcvS3t_A/QXLEXwt9i= vAJ > > > > https://stackoverflow.com/questions/1820451/ruby-style-how-to-check-wh= ether-a-nested-hash-element-exists > > > > https://stackoverflow.com/questions/19115838/how-do-i-use-the-fetch-me= thod-for-nested-hash > > > > https://stackoverflow.com/questions/10130726/ruby-access-multidimensio= nal-hash-and-avoid-access-nil-object > > > > [2] http://stackoverflow.com/a/1820492/283398 > > > > > > > > -- > > https://bugs.ruby-lang.org/ > > >=20=20 >=20=20 >=20=20 > --=20 > Austin Ziegler =E2=80=A2 halostatue@gmail.com =E2=80=A2 austin@halostatu= e.ca > http://www.halostatue.ca/ =E2=80=A2 http://twitter.com/halostatue ---------------------------------------- Feature #11643: A new method on Hash to grab values out of nested hashes, f= ailing gracefully https://bugs.ruby-lang.org/issues/11643#change-54752 * Author: Gabe Kopley * Status: Open * Priority: Normal * Assignee:=20 ---------------------------------------- (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 ht= tps://bugs.ruby-lang.org/issues/11537 ) This comes up sometimes in Rails programming [1]: `params[:order] && params[:order][:shipping_info] && params[:order][:shippi= ng_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 semanti= cally? 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 (A= lthough 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-h= ash-and-avoid-access-nil-object [2] http://stackoverflow.com/a/1820492/283398 --=20 https://bugs.ruby-lang.org/