From ea5184b939ec3dbdcbd7013da350b8dcb6ca6107 Mon Sep 17 00:00:00 2001 From: mrkn Date: Tue, 9 Aug 2016 08:54:15 +0000 Subject: hash.c: implement Hash#map_v and Hash#map_v! * hash.c (rb_hash_map_v, rb_hash_map_v_bang): impelement Hash#map_v and Hash#map_v! [Feature #12512] [ruby-core:76095] * test/ruby/test_hash.rb: add tests for above change. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55847 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/test_hash.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'test/ruby/test_hash.rb') diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index 6a8e7f0423..2b2f154803 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -1415,6 +1415,27 @@ class TestHash < Test::Unit::TestCase assert_equal([10, 20, 30], [1, 2, 3].map(&h)) end + def test_map_v + x = @cls[a: 1, b: 2, c: 3] + y = x.map_v {|v| v ** 2 } + assert_equal([1, 4, 9], y.values_at(:a, :b, :c)) + assert_not_same(x, y) + + y = x.map_v.with_index {|v, i| "#{v}.#{i}" } + assert_equal(%w(1.0 2.1 3.2), y.values_at(:a, :b, :c)) + end + + def test_map_v_bang + x = @cls[a: 1, b: 2, c: 3] + y = x.map_v! {|v| v ** 2 } + assert_equal([1, 4, 9], y.values_at(:a, :b, :c)) + assert_same(x, y) + + x = @cls[a: 1, b: 2, c: 3] + y = x.map_v!.with_index {|v, i| "#{v}.#{i}" } + assert_equal(%w(1.0 2.1 3.2), y.values_at(:a, :b, :c)) + end + class TestSubHash < TestHash class SubHash < Hash def reject(*) -- cgit v1.2.3