ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:71293] [Ruby trunk - Feature #11643] [Open] A new method on Hash to grab values out of nested hashes, failing gracefully
       [not found] <redmine.issue-11643.20151102020453@ruby-lang.org>
@ 2015-11-02  2:04 ` gabe
  2015-11-02  4:07 ` [ruby-core:71294] [Ruby trunk - Feature #11643] " matthew
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: gabe @ 2015-11-02  2:04 UTC (permalink / raw)
  To: ruby-core

Issue #11643 has been reported by Gabe Kopley.

----------------------------------------
Feature #11643: A new method on Hash to grab values out of nested hashes, failing gracefully
https://bugs.ruby-lang.org/issues/11643

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:71294] [Ruby trunk - Feature #11643] A new method on Hash to grab values out of nested hashes, failing gracefully
       [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 ` matthew
  2015-11-02 20:36 ` [ruby-core:71300] " gabe
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: matthew @ 2015-11-02  4:07 UTC (permalink / raw)
  To: ruby-core

Issue #11643 has been updated by Matthew Kerwin.


How about:

    params.?[:order].?[shipping_info].?[country]

----------------------------------------
Feature #11643: A new method on Hash to grab values out of nested hashes, failing gracefully
https://bugs.ruby-lang.org/issues/11643#change-54668

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:71300] [Ruby trunk - Feature #11643] A new method on Hash to grab values out of nested hashes, failing gracefully
       [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 ` gabe
  2015-11-03  6:21 ` [ruby-core:71307] " matz
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: gabe @ 2015-11-02 20:36 UTC (permalink / raw)
  To: ruby-core

Issue #11643 has been updated by Gabe Kopley.


Matthew Kerwin wrote:
> How about:
> 
>     params.?[:order].?[shipping_info].?[country]

Thanks Matthew, I'll be honest, I hadn't thought of that. There is a certain appeal in avoiding adding a new method on Hash. On the other hand, by adding a new method we can more easily and more beautifully do metaprogramming, use a potentially more concise expression, convey more rich semantics, and possibly reduce the number of method calls.

----------------------------------------
Feature #11643: A new method on Hash to grab values out of nested hashes, failing gracefully
https://bugs.ruby-lang.org/issues/11643#change-54672

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:71307] [Ruby trunk - Feature #11643] A new method on Hash to grab values out of nested hashes, failing gracefully
       [not found] <redmine.issue-11643.20151102020453@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2015-11-02 20:36 ` [ruby-core:71300] " gabe
@ 2015-11-03  6:21 ` matz
  2015-11-03  7:06 ` [ruby-core:71309] " hanmac
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: matz @ 2015-11-03  6:21 UTC (permalink / raw)
  To: ruby-core

Issue #11643 has been updated by Yukihiro Matsumoto.


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.



----------------------------------------
Feature #11643: A new method on Hash to grab values out of nested hashes, failing gracefully
https://bugs.ruby-lang.org/issues/11643#change-54682

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:71309] [Ruby trunk - Feature #11643] A new method on Hash to grab values out of nested hashes, failing gracefully
       [not found] <redmine.issue-11643.20151102020453@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2015-11-03  6:21 ` [ruby-core:71307] " matz
@ 2015-11-03  7:06 ` hanmac
  2015-11-04  0:03 ` [ruby-core:71323] " dsisnero
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: hanmac @ 2015-11-03  7:06 UTC (permalink / raw)
  To: ruby-core

Issue #11643 has been updated by Hans Mackowiak.


dam i begun to like that "params.?[:order]" bad that it got reverted :/
i think the problem is that it might parse "?[" as a char or something?

----------------------------------------
Feature #11643: A new method on Hash to grab values out of nested hashes, failing gracefully
https://bugs.ruby-lang.org/issues/11643#change-54684

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:71323] [Ruby trunk - Feature #11643] A new method on Hash to grab values out of nested hashes, failing gracefully
       [not found] <redmine.issue-11643.20151102020453@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2015-11-03  7:06 ` [ruby-core:71309] " hanmac
@ 2015-11-04  0:03 ` dsisnero
  2015-11-07  4:57   ` [ruby-core:71378] " Austin Ziegler
  2015-11-07 19:53 ` [ruby-core:71380] " keithrbennett
                   ` (3 subsequent siblings)
  9 siblings, 1 reply; 11+ messages in thread
From: dsisnero @ 2015-11-04  0:03 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:71378] Re: [Ruby trunk - Feature #11643] A new method on Hash to grab values out of nested hashes, failing gracefully
  2015-11-04  0:03 ` [ruby-core:71323] " dsisnero
@ 2015-11-07  4:57   ` Austin Ziegler
  0 siblings, 0 replies; 11+ messages in thread
From: Austin Ziegler @ 2015-11-07  4:57 UTC (permalink / raw)
  To: Ruby developers

[-- Attachment #1: Type: text/plain, Size: 3781 bytes --]

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

[-- Attachment #2: Type: text/html, Size: 6372 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:71380] [Ruby trunk - Feature #11643] A new method on Hash to grab values out of nested hashes, failing gracefully
       [not found] <redmine.issue-11643.20151102020453@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2015-11-04  0:03 ` [ruby-core:71323] " dsisnero
@ 2015-11-07 19:53 ` keithrbennett
  2015-11-09  6:56 ` [ruby-core:71397] " matz
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: keithrbennett @ 2015-11-07 19:53 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:71397] [Ruby trunk - Feature #11643] A new method on Hash to grab values out of nested hashes, failing gracefully
       [not found] <redmine.issue-11643.20151102020453@ruby-lang.org>
                   ` (6 preceding siblings ...)
  2015-11-07 19:53 ` [ruby-core:71380] " keithrbennett
@ 2015-11-09  6:56 ` matz
  2015-11-09  7:01 ` [ruby-core:71398] " ko1
  2015-11-09  7:02 ` [ruby-core:71399] " hanmac
  9 siblings, 0 replies; 11+ messages in thread
From: matz @ 2015-11-09  6:56 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:71398] [Ruby trunk - Feature #11643] A new method on Hash to grab values out of nested hashes, failing gracefully
       [not found] <redmine.issue-11643.20151102020453@ruby-lang.org>
                   ` (7 preceding siblings ...)
  2015-11-09  6:56 ` [ruby-core:71397] " matz
@ 2015-11-09  7:01 ` ko1
  2015-11-09  7:02 ` [ruby-core:71399] " hanmac
  9 siblings, 0 replies; 11+ messages in thread
From: ko1 @ 2015-11-09  7:01 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:71399] [Ruby trunk - Feature #11643] A new method on Hash to grab values out of nested hashes, failing gracefully
       [not found] <redmine.issue-11643.20151102020453@ruby-lang.org>
                   ` (8 preceding siblings ...)
  2015-11-09  7:01 ` [ruby-core:71398] " ko1
@ 2015-11-09  7:02 ` hanmac
  9 siblings, 0 replies; 11+ messages in thread
From: hanmac @ 2015-11-09  7:02 UTC (permalink / raw)
  To: ruby-core

Issue #11643 has been updated by Hans Mackowiak.


hm shortly patch idea: instead of 

~~~
keys = keys.dup
ckey = keys.shift
~~~

whouldn't

~~~
ckey, *keys = keys
~~~

be better?


----------------------------------------
Feature #11643: A new method on Hash to grab values out of nested hashes, failing gracefully
https://bugs.ruby-lang.org/issues/11643#change-54768

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2015-11-09  6:33 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [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 ` [ruby-core:71398] " ko1
2015-11-09  7:02 ` [ruby-core:71399] " hanmac

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).