From: ko1@atdot.net
To: ruby-core@ruby-lang.org
Subject: [ruby-core:71398] [Ruby trunk - Feature #11643] A new method on Hash to grab values out of nested hashes, failing gracefully
Date: Mon, 09 Nov 2015 07:01:44 +0000 [thread overview]
Message-ID: <redmine.journal-54767.20151109070143.2bbe9df9dfca2f6e@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-11643.20151102020453@ruby-lang.org
Issue #11643 has been updated by Koichi Sasada.
Discussion: https://docs.google.com/document/d/1D0Eo5N7NE_unIySOKG9lVj_eyXf66BQPM4PKp7NvMyQ/pub
Feel free to continue discussion on this ticket.
----------------------------------------
Feature #11643: A new method on Hash to grab values out of nested hashes, failing gracefully
https://bugs.ruby-lang.org/issues/11643#change-54767
* 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-09 6:32 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 ` [ruby-core:71380] " keithrbennett
2015-11-09 6:56 ` [ruby-core:71397] " matz
2015-11-09 7:01 ` ko1 [this message]
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-54767.20151109070143.2bbe9df9dfca2f6e@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).