add #dig to Array,Hash and Struct

This commit is contained in:
Yukihiro "Matz" Matsumoto
2016-03-23 11:49:28 +09:00
parent 4c1ce0f61c
commit daf83946bb
3 changed files with 51 additions and 0 deletions
+17
View File
@@ -744,4 +744,21 @@ class Array
def to_ary
self
end
##
# call-seq:
# ary.dig(idx, ...) -> object
#
# Extracts the nested value specified by the sequence of <i>idx</i>
# objects by calling +dig+ at each step, returning +nil+ if any
# intermediate step is +nil+.
#
def dig(idx,*args)
n = self[idx]
if args.size > 0
n&.dig(*args)
else
n
end
end
end