From 29862685c0acf3a40c6b1f9e8780cbbd86cba658 Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 9 Nov 2015 12:27:26 +0000 Subject: 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 --- test/ruby/test_array.rb | 6 ++++++ test/ruby/test_hash.rb | 6 ++++++ 2 files changed, 12 insertions(+) (limited to 'test/ruby') diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 74da8c1dab..0922cb4c65 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -2651,6 +2651,12 @@ class TestArray < Test::Unit::TestCase end end + def test_dig + h = @cls[@cls[{a: 1}], 0] + assert_equal(1, h.dig(0, 0, :a)) + assert_nil(h.dig(1, 0)) + end + private def need_continuation unless respond_to?(:callcc, true) diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index e8f0f87326..ee51c8ed7d 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -1302,6 +1302,12 @@ class TestHash < Test::Unit::TestCase x = x end + def test_dig + h = @cls[a: @cls[b: [1, 2, 3]], c: 4] + assert_equal(1, h.dig(:a, :b, 0)) + assert_nil(h.dig(:c, 1)) + end + class TestSubHash < TestHash class SubHash < Hash def reject(*) -- cgit v1.2.3