From: keithrbennett@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:71380] [Ruby trunk - Feature #11643] A new method on Hash to grab values out of nested hashes, failing gracefully
Date: Sat, 07 Nov 2015 19:53:51 +0000 [thread overview]
Message-ID: <redmine.journal-54752.20151107195351.792db0a2bd694426@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-11643.20151102020453@ruby-lang.org
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 Rubyists
* 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.
>
> ```ruby
> class Hash
> def fetch_in(*keys, **kwargs, &block)
> keys = keys.dup
> ckey = 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 = 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 = {
> a: {
> b: {
> c: :d
> }
> }
> }
>
> def y
> yield
> rescue => 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 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/
> >
>
>
>
> --
> Austin Ziegler • halostatue@gmail.com • austin@halostatue.ca
> http://www.halostatue.ca/ • http://twitter.com/halostatue
----------------------------------------
Feature #11643: A new method on Hash to grab values out of nested hashes, failing gracefully
https://bugs.ruby-lang.org/issues/11643#change-54752
* 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/
next prev parent reply other threads:[~2015-11-07 19:24 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-11643.20151102020453@ruby-lang.org>
2015-11-02 2:04 ` [ruby-core:71293] [Ruby trunk - Feature #11643] [Open] A new method on Hash to grab values out of nested hashes, failing gracefully gabe
2015-11-02 4:07 ` [ruby-core:71294] [Ruby trunk - Feature #11643] " matthew
2015-11-02 20:36 ` [ruby-core:71300] " gabe
2015-11-03 6:21 ` [ruby-core:71307] " matz
2015-11-03 7:06 ` [ruby-core:71309] " hanmac
2015-11-04 0:03 ` [ruby-core:71323] " dsisnero
2015-11-07 4:57 ` [ruby-core:71378] " Austin Ziegler
2015-11-07 19:53 ` keithrbennett [this message]
2015-11-09 6:56 ` [ruby-core:71397] " matz
2015-11-09 7:01 ` [ruby-core:71398] " ko1
2015-11-09 7:02 ` [ruby-core:71399] " hanmac
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-54752.20151107195351.792db0a2bd694426@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).