[#71439] [Ruby trunk - Feature #11339] [PATCH] io.c: avoid kwarg parsing in C API — matz@...
Issue #11339 has been updated by Yukihiro Matsumoto.
7 messages
2015/11/11
[#71473] Re: [Ruby trunk - Feature #11339] [PATCH] io.c: avoid kwarg parsing in C API
— Eric Wong <normalperson@...>
2015/11/13
Entire series for sockets
[#71450] Ruby 2.3.0-preview1 Released — "NARUSE, Yui" <naruse@...>
Hi,
5 messages
2015/11/11
[#71617] [Ruby trunk - Feature #11664] [PATCH] introduce rb_autoload_value to replace rb_autoload — nobu@...
Issue #11664 has been updated by Nobuyoshi Nakada.
3 messages
2015/11/20
[#71721] [Ruby trunk - Feature #11741] Migrate Ruby to Git from Subversion — me@...
Issue #11741 has been updated by Jon Moss.
4 messages
2015/11/28
[ruby-core:71397] [Ruby trunk - Feature #11643] A new method on Hash to grab values out of nested hashes, failing gracefully
From:
matz@...
Date:
2015-11-09 06:56:10 UTC
List:
ruby-core #71397
Issue #11643 has been updated by Yukihiro Matsumoto.
The idea is accepted. The name is the problem. The current candidates are 'dig' and 'fetch_in'.
I prefer 'dig'. If you have any other idea, please propose.
Matz.
----------------------------------------
Feature #11643: A new method on Hash to grab values out of nested hashes, failing gracefully
https://bugs.ruby-lang.org/issues/11643#change-54766
* 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/