From: Gabe Kopley Date: 2014-11-06T17:16:13-08:00 Subject: [ruby-core:66122] A new method on Hash to grab values out of nested hashes, failing gracefully? --089e0149caa61471b605073a8f60 Content-Type: text/plain; charset=UTF-8 Hi folks, This comes up sometimes in Rails programming [0]: 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? 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 [1]: 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) Would a method like this possibly belong in Ruby core, or in Active Support extensions, or is it just an all-around terrible idea? Thanks, Gabe [0] Sources: 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 [1] http://stackoverflow.com/a/1820492/283398 --089e0149caa61471b605073a8f60 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi folks,

This comes up sometimes in Ra= ils programming [0]:

=C2=A0 params[:order] &&am= p; params[:order][:shipping_info] && params[:order][:shipping_info]= [:country]

or
<= br>
=C2=A0 params[:order][:shipping_info][:country] rescue nil=

or

=C2=A0 params.fetch(:order, = {}).fetch(:shipping_info, {}).fetch(:country, nil)
= <= br>
What if Hash gave us a method to accomplish this more c= oncisely?

Eg.

=C2=A0=C2=A0params.traverse_nested_hashes_and_return_nil= _if_a_key_isnt_found(:order, :shipping_info, :country)

=
Or to take a nice method name suggestion [1]:
=

=C2=A0=C2=A0params.dig(:order, :shipping_inf= o, :country)

Another example= solution is=C2=A0https://github.com/intridea/hashie#deepfetch (Although I don&= #39;t like "fetch" in this method name since it doesn't and c= an't take a default value as an argument like Hash#fetch does)

Would a method like this possibly belon= g in Ruby core, or in Active Support extensions, or is it just an all-aroun= d terrible idea?

Thanks,
<= div>Gabe

[0] Sources:=
https://groups.google.com/d/msg/rubyonrails-core/b= OkvcvS3t_A/QXLEXwt9ivAJ
https://stackoverflow.com/questions/= 1820451/ruby-style-how-to-check-whether-a-nested-hash-element-exists
https://stackoverflow.co= m/questions/19115838/how-do-i-use-the-fetch-method-for-nested-hash
<= /div>

[1]=C2=A0http://stackoverflow.com/a/1820492/283398
--089e0149caa61471b605073a8f60--