[ruby-core:71378] Re: [Ruby trunk - Feature #11643] A new method on Hash to grab values out of nested hashes, failing gracefully

From: Austin Ziegler <halostatue@...>
Date: 2015-11-07 04:57:15 UTC
List: ruby-core #71378
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, <dsisnero@gmail.com> 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

In This Thread

Prev Next