summaryrefslogtreecommitdiffhomepage
path: root/mrbgems
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2016-03-23 11:49:28 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2016-03-23 11:49:28 +0900
commitdaf83946bbf2834da8393cd50af2bc7bcee3289f (patch)
tree6fa05f86dcfa6a6b984fb515a7482448cf5a676a /mrbgems
parent4c1ce0f61cfc8988fa50dd48bb404f01d234eb7b (diff)
downloadmruby-daf83946bbf2834da8393cd50af2bc7bcee3289f.tar.gz
mruby-daf83946bbf2834da8393cd50af2bc7bcee3289f.zip
add #dig to Array,Hash and Struct
Diffstat (limited to 'mrbgems')
-rw-r--r--mrbgems/mruby-array-ext/mrblib/array.rb17
-rw-r--r--mrbgems/mruby-hash-ext/mrblib/hash.rb17
-rw-r--r--mrbgems/mruby-struct/mrblib/struct.rb17
3 files changed, 51 insertions, 0 deletions
diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb
index f19581cdc..04637471d 100644
--- a/mrbgems/mruby-array-ext/mrblib/array.rb
+++ b/mrbgems/mruby-array-ext/mrblib/array.rb
@@ -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
diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb
index 2727e1f65..db4db42e8 100644
--- a/mrbgems/mruby-hash-ext/mrblib/hash.rb
+++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb
@@ -365,4 +365,21 @@ class Hash
key?(key) and self[key] == val
}
end
+
+ ##
+ # call-seq:
+ # hsh.dig(key,...) -> object
+ #
+ # Extracts the nested value specified by the sequence of <i>key</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
diff --git a/mrbgems/mruby-struct/mrblib/struct.rb b/mrbgems/mruby-struct/mrblib/struct.rb
index 1445392f3..7cf3dd3ab 100644
--- a/mrbgems/mruby-struct/mrblib/struct.rb
+++ b/mrbgems/mruby-struct/mrblib/struct.rb
@@ -82,5 +82,22 @@ if Object.const_defined?(:Struct)
#
alias to_s inspect
end
+
+ ##
+ # call-seq:
+ # hsh.dig(key,...) -> object
+ #
+ # Extracts the nested value specified by the sequence of <i>key</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