14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/polyfill/v2_3/array.rb', line 14
def dig(head, *rest)
[head, *rest].reduce(self) do |value, accessor|
next_value =
case value
when ::Array
value.at(accessor)
when ::Hash
value[accessor]
when ::Struct
value[accessor] if value.members.include?(accessor)
else
begin
break value.dig(*rest)
rescue NoMethodError
raise TypeError, "#{value.class} does not have a #dig method"
end
end
break nil if next_value.nil?
next_value
end
end
|