From b25e27277dc39f25cfca4db8452d254f6cc8046e Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Thu, 26 Dec 2019 15:50:34 +0900 Subject: Transform hash keys by a hash [Feature #16274] --- test/ruby/test_hash.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'test/ruby') diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index ccc3355930..733bccd138 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -1638,6 +1638,9 @@ class TestHash < Test::Unit::TestCase y = x.transform_keys.with_index {|k, i| "#{k}.#{i}" } assert_equal(%w(a.0 b.1 c.2), y.keys) + + assert_equal({A: 1, B: 2, c: 3}, x.transform_keys({a: :A, b: :B, d: :D})) + assert_equal({A: 1, B: 2, "c" => 3}, x.transform_keys({a: :A, b: :B, d: :D}, &:to_s)) end def test_transform_keys_bang @@ -1660,6 +1663,13 @@ class TestHash < Test::Unit::TestCase x = @cls[true => :a, false => :b] x.transform_keys! {|k| !k } assert_equal([false, :a, true, :b], x.flatten) + + x = @cls[a: 1, b: 2, c: 3] + x.transform_keys!({a: :A, b: :B, d: :D}) + assert_equal({A: 1, B: 2, c: 3}, x) + x = @cls[a: 1, b: 2, c: 3] + x.transform_keys!({a: :A, b: :B, d: :D}, &:to_s) + assert_equal({A: 1, B: 2, "c" => 3}, x) end def test_transform_values -- cgit v1.2.3