Project

General

Profile

Actions

Bug #11762

closed

Array#dig can raise TypeError: no implicit conversion of Symbol/String into Integer

Added by colindkelley (Colin Kelley) over 8 years ago. Updated about 8 years ago.

Status:
Closed
Target version:
-
ruby -v:
2.3.0-preview2
[ruby-core:71798]

Description

If you try to dig in an Array using a symbol or string, a TypeError exception will be raised:

irb> ['zero', 'one', 'two'].dig(:first)
TypeError: no implicit conversion of Symbol into Integer
from (irb):1:in `dig'
from (irb):1

I think it should return nil in this case. The most typical use case for dig is to dig through parsed JSON and either find the result we expected or else nil. Wouldn't it defeat the purpose of dig if we had to wrap calls to it in a rescue to handle the case that an Array was present where we expected a Hash?

Can we clarify the desired behavior for this case, then update the documentation and tests to reflect that?


Files

11762.patch (3.19 KB) 11762.patch colindkelley (Colin Kelley), 12/08/2015 04:02 AM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like1Like0Like0Like0Like0Like0