summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-12-08 05:21:11 +0000
committermarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-12-08 05:21:11 +0000
commitcb3b463a50dee9d4e3d174dc9b1d7c8181678cd4 (patch)
tree0e7faca165921fba08b4e438f23049ef400dff99
parent1fadd438811426523a79ccea93a45f0a202a50f6 (diff)
* array.c: Improve and fix documentation for Array#dig
[#11776] * hash.c: ditto * struct.c: ditto * test_hash.rb: Add basic test for user defined `dig`. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52941 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--array.c9
-rw-r--r--hash.c10
-rw-r--r--struct.c5
-rw-r--r--test/ruby/test_hash.rb6
4 files changed, 22 insertions, 8 deletions
diff --git a/array.c b/array.c
index 159dc8f7a47..dd14837b762 100644
--- a/array.c
+++ b/array.c
@@ -5535,13 +5535,16 @@ rb_ary_any_p(VALUE ary)
* call-seq:
* ary.dig(idx, ...) -> object
*
- * Extracts the nested array value specified by the sequence of <i>idx</i>
- * objects.
+ * 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+.
*
* a = [[1, [2, 3]]]
*
* a.dig(0, 1, 1) #=> 3
- * a.dig(0, 0, 0) #=> nil
+ * a.dig(1, 2, 3) #=> nil
+ * a.dig(0, 0, 0) #=> NoMethodError, undefined method `dig' for 1:Fixnum
+ * [42, {foo: :bar}].dig(1, :foo) #=> :bar
*/
VALUE
diff --git a/hash.c b/hash.c
index b0489502b04..1fd9d12e158 100644
--- a/hash.c
+++ b/hash.c
@@ -2695,13 +2695,17 @@ rb_hash_any_p(VALUE hash)
* call-seq:
* hsh.dig(key, ...) -> object
*
- * Extracts the nested hash value specified by the sequence of <i>key</i>
- * objects.
+ * 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+.
*
* h = { foo: {bar: {baz: 1}}}
*
* h.dig(:foo, :bar, :baz) #=> 1
- * h.dig(:foo, :zot) #=> nil
+ * h.dig(:foo, :zot, :xyz) #=> nil
+ *
+ * g = { foo: [10, 11, 12] }
+ * g.dig(:foo, 1) #=> 11
*/
VALUE
diff --git a/struct.c b/struct.c
index cebc445a437..a2aa708eff4 100644
--- a/struct.c
+++ b/struct.c
@@ -1130,8 +1130,9 @@ rb_struct_size(VALUE s)
* call-seq:
* struct.dig(key, ...) -> object
*
- * Extracts the nested struct value specified by the sequence of <i>key</i>
- * objects.
+ * 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+.
*
* klass = Struct.new(:a)
* o = klass.new(klass.new({b: [1, 2, 3]}))
diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb
index 77417ecbe0b..c08908fa760 100644
--- a/test/ruby/test_hash.rb
+++ b/test/ruby/test_hash.rb
@@ -1306,6 +1306,12 @@ class TestHash < Test::Unit::TestCase
h = @cls[a: @cls[b: [1, 2, 3]], c: 4]
assert_equal(1, h.dig(:a, :b, 0))
assert_nil(h.dig(:c, 1))
+ o = Object.new
+ def o.dig(*args)
+ {dug: args}
+ end
+ h[:d] = o
+ assert_equal({dug: [:foo, :bar]}, h.dig(:d, :foo, :bar))
end
def test_cmp