diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-11-09 12:27:26 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-11-09 12:27:26 +0000 |
commit | 29862685c0acf3a40c6b1f9e8780cbbd86cba658 (patch) | |
tree | 64813c6d3e206f9ad51009a640750f26ce9e7231 /array.c | |
parent | 50dbcf5f6d56b95fd1c240341b5b2615715b2720 (diff) | |
download | ruby-29862685c0acf3a40c6b1f9e8780cbbd86cba658.tar.gz |
dig
* array.c (rb_ary_dig): new method Array#dig.
* hash.c (rb_hash_dig): new method Hash#dig.
* object.c (rb_obj_dig): dig in nested arrays/hashes.
[Feature #11643]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52504 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r-- | array.c | 26 |
1 files changed, 25 insertions, 1 deletions
@@ -1302,7 +1302,7 @@ rb_ary_aref(int argc, const VALUE *argv, VALUE ary) * a.at(-1) #=> "e" */ -static VALUE +VALUE rb_ary_at(VALUE ary, VALUE pos) { return rb_ary_entry(ary, NUM2LONG(pos)); @@ -5532,6 +5532,29 @@ rb_ary_any_p(VALUE ary) } /* + * call-seq: + * ary.dig(idx, ...) -> object + * + * Retrieves the value object corresponding to the each <i>idx</i> + * objects repeatedly. + * + * a = [[1, [2, 3]]] + * + * a.dig(0, 1, 1) #=> 3 + * a.dig(0, 0, 0) #=> nil + */ + +VALUE +rb_ary_dig(int argc, VALUE *argv, VALUE self) +{ + rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS); + self = rb_ary_at(self, *argv); + if (!--argc) return self; + ++argv; + return rb_obj_dig(argc, argv, self, Qnil); +} + +/* * Arrays are ordered, integer-indexed collections of any object. * * Array indexing starts at 0, as in C or Java. A negative index is assumed @@ -5882,6 +5905,7 @@ Init_Array(void) rb_define_method(rb_cArray, "bsearch", rb_ary_bsearch, 0); rb_define_method(rb_cArray, "bsearch_index", rb_ary_bsearch_index, 0); rb_define_method(rb_cArray, "any?", rb_ary_any_p, 0); + rb_define_method(rb_cArray, "dig", rb_ary_dig, -1); id_cmp = rb_intern("<=>"); id_random = rb_intern("random"); |