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 8E0F519C003E for ; Sat, 7 Nov 2015 13:28:32 +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 38889B5D8CE for ; Sat, 7 Nov 2015 13:57:43 +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 58D8218CC7B1 for ; Sat, 7 Nov 2015 13:57:43 +0900 (JST) Received: from [221.186.184.76] (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 2D7A8120482; Sat, 7 Nov 2015 13:57:41 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from mail-oi0-f52.google.com (mail-oi0-f52.google.com [209.85.218.52]) by neon.ruby-lang.org (Postfix) with ESMTPS id BD6E912045D for ; Sat, 7 Nov 2015 13:57:36 +0900 (JST) Received: by oies6 with SMTP id s6so34207091oie.1 for ; Fri, 06 Nov 2015 20:57:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=PcYXzUZwupRezDbrm+/fNuPqUMtUp6CABgM7/qZLVwY=; b=miwCIVhDV5TlL6jjJrk2JWfl4WRG3a+WLxG1sNYfyqVaYaj7GUUujQkyiy993q6oM7 fo58pBlZker7l49w8dGWbcEIlWdxr0XOAtoK6d1UeGsShs6+EbwYVf1afOBgDsmYw7rS 5OrBxuVdgrkZ6264Im2JpE9YsezqKd+BcIHQOMpAIcgXUkTL5G97YFD9px6X2xA3vkNB 1cab0uMzz0KoX29fJsPJfBIJgV7qD911ggIjtxQrhNdomaMMSA/Kp9DNbIOwF0mB1F8v hHBMt+2nckxL52fOUl0cZaKCSfiyqXd+Gfoxlg1KelA1yGEJFzY0lY/YgcC11FXMBAW6 vAmA== X-Received: by 10.202.172.79 with SMTP id v76mr10158110oie.79.1446872254780; Fri, 06 Nov 2015 20:57:34 -0800 (PST) MIME-Version: 1.0 Received: by 10.202.93.7 with HTTP; Fri, 6 Nov 2015 20:57:15 -0800 (PST) In-Reply-To: References: From: Austin Ziegler Date: Fri, 6 Nov 2015 23:57:15 -0500 Message-ID: To: Ruby developers Content-Type: multipart/alternative; boundary=001a113ce5dec81b140523ec32f5 X-ML-Name: ruby-core X-Mail-Count: 71378 Subject: [ruby-core:71378] Re: [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" --001a113ce5dec81b140523ec32f5 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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. ```ruby class Hash def fetch_in(*keys, **kwargs, &block) keys =3D keys.dup ckey =3D keys.shift 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 child =3D self[ckey] 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 a =3D { a: { b: { c: :d } } } def y yield rescue =3D> e e end 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' } } ``` As a proposed name, I suggest `locate`. On Tue, Nov 3, 2015 at 7:03 PM, wrote: > 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 though= t. > > 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-wheth= er-a-nested-hash-element-exists > > https://stackoverflow.com/questions/19115838/how-do-i-use-the-fetch-metho= d-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/ > --=20 Austin Ziegler =E2=80=A2 halostatue@gmail.com =E2=80=A2 austin@halostatue.c= a http://www.halostatue.ca/ =E2=80=A2 http://twitter.com/halostatue --001a113ce5dec81b140523ec32f5 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
The problem with `hash.fetch_in(:order, :shipping_info, :c= ountry, 'Not found')` is that `'Not found'` is a (possibly)= valid key. You would need to implement this with a kwarg.

```ruby
class Hash
=C2=A0 def fetch_in(*key= s, **kwargs, &block)
=C2=A0 =C2=A0 keys =3D keys.dup
=C2=A0 =C2=A0 ckey =3D keys.shift

=C2=A0 =C2=A0 = unless self.key?(ckey)
=C2=A0 =C2=A0 =C2=A0 return kwargs[:defaul= t] if kwargs.key?(:default)
=C2=A0 =C2=A0 =C2=A0 return block.cal= l(ckey) if block
=C2=A0 =C2=A0 =C2=A0 fail KeyError, "key no= t found #{ckey.inspect}"
=C2=A0 =C2=A0 end

=C2=A0 =C2=A0 child =3D self[ckey]

=C2=A0 = =C2=A0 if keys.empty?
=C2=A0 =C2=A0 =C2=A0 child
=C2=A0= =C2=A0 elsif child.respond_to?(:fetch_in)
=C2=A0 =C2=A0 =C2=A0 c= hild.fetch_in(*keys, **kwargs, &block)
=C2=A0 =C2=A0 else
=C2=A0 =C2=A0 =C2=A0 fail ArgumentError, 'more keys than Hashes&= #39;
=C2=A0 =C2=A0 end
=C2=A0 end
end

a =3D {
=C2=A0 a: {
=C2=A0 =C2=A0 b: = {
=C2=A0 =C2=A0 =C2=A0 c: :d
=C2=A0 =C2=A0 }
= =C2=A0 }
}

def y
=C2=A0 yield<= /div>
rescue =3D> e
=C2=A0 e
end

<= /div>
p y { a }
p y { a.fetch_in(:a) }
p y { a.fetc= h_in(:a, :b) }
p y { a.fetch_in(:a, :b, :c) }
p y { a.f= etch_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.fe= tch_in(:a, :b, :d) { 'z' } }
```

As a proposed name, I suggest `locate`.

On Tue, Nov 3, 2015 at 7:03 PM, <dsis= nero@gmail.com> wrote:
Issu= e #11643 has been updated by Dominic Sisneros.


Yukihiro Matsumoto wrote:
> I prefer method way to (already reverted) `params.?[:order].?[:shippin= g_info].?[:country]`.
> I am not sure `dig` is the best name for it. It's short, concise t= hought.
> Any other idea, anyone?
>
> Matz.

clojure has get-in for their maps, how about fetch_in with replaceme= nt 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, f= ailing gracefully
https://bugs.ruby-lang.org/issues/11643#c= hange-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 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 (Although I don't like "fetch" in this m= ethod name since it doesn't and can't take a default value as an ar= gument like Hash#fetch does)

What do you all think?


[0] https://groups.google.com/fo= rum/#!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-whe= ther-a-nested-hash-element-exists
https://st= ackoverflow.com/questions/19115838/how-do-i-use-the-fetch-method-for-nested= -hash
https://stackoverflow.com/questions/10130726/ruby-access-multidimensi= onal-hash-and-avoid-access-nil-object

[2] http://stackoverflow.com/a/1820492/283398



--
https://bugs.ruby-lang.org/



--
=
Austin Ziegler =E2=80=A2 halostatue@gmail.com =E2=80=A2 <= a href=3D"mailto:austin@halostatue.ca" target=3D"_blank">austin@halostatue.= ca
http://ww= w.halostatue.ca/ =E2=80=A2 http://twitter.com/halostatue
--001a113ce5dec81b140523ec32f5--