From: Colin Kelley Date: 2015-11-29T15:24:33-08:00 Subject: [ruby-core:71738] behavior of Array#dig when passed a non-numeric index? --94eb2c055098e50f230525b63bf3 Content-Type: text/plain; charset=UTF-8 Hello, I am working on this gem to provide backward-compatibility of the new `dig` method in `Hash` and `Array`. https://rubygems.org/gems/ruby_dig (See the accompanying blog post for more info. http://development.invoca.com/ruby_dig-gem-adds-hash-dig-and-array-dig-from-ruby-2-3-to-earlier-versions/ ) But I can't tell from the documentation or tests in the original commit https://github.com/ruby/ruby/commit/29862685c0acf3a40c6b1f9e8780cbbd86cba658 what is supposed to happen if Array#dig is passed a non-numeric index. For example: ['zero', 'one', 'two'].dig(:first) It seems like the C code is just calling the `[]` operator, in which case it would raise this exception: TypeError: no implicit conversion of Symbol into Integer But it definitely seems like the spirit of `dig` that `nil` should be returned if the expected structure isn't there. I believe the most typical use case for `dig` is to dig through a JSON hash and either find the result we expected, or get `nil`. Wouldn't it defeat the purpose of `dig` if we had to wrap it in a `rescue`? Can we clarify the desired behavior, then update the documentation and tests to reflect that? -Colin --94eb2c055098e50f230525b63bf3 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hello,

I am working on this gem to prov= ide backward-compatibility of the new `dig` method in `Hash` and `Array`.= =C2=A0

=C2=A0 =C2=A0 https://rubygems.org/gems/ruby_dig

=
(See the accompanying blog post for more info. =C2=A0http://development.invoca.com/ruby_dig-gem-= adds-hash-dig-and-array-dig-from-ruby-2-3-to-earlier-versions/ )
<= div>
But I can't tell from the documentation or tests in = the original commit

=C2=A0 =C2=A0 ht= tps://github.com/ruby/ruby/commit/29862685c0acf3a40c6b1f9e8780cbbd86cba658<= /a>

what is supposed to happen if Array#dig is= passed a non-numeric index. For example:

=C2=A0 =C2=A0 ['zero', 'one', &= #39;two'].dig(:first)

It seems like the= C code is just calling the `[]` operator, in which case it would raise thi= s exception:

=C2=A0 = =C2=A0 TypeError: no implicit conversion of Symbol into Integer

But it definitely seems like the spirit of `dig` that `nil= ` should be returned if the expected structure isn't there.=C2=A0 I bel= ieve the most typical use case for `dig` is to dig through a JSON hash and = either find the result we expected, or get `nil`.=C2=A0 Wouldn't it def= eat the purpose of `dig` if we had to wrap it in a `rescue`?

=
Can we clarify the desired behavior, then update the documentati= on and tests to reflect that?

-Colin
--94eb2c055098e50f230525b63bf3--