Define transform_keys! in HashWithIndifferentAccess

Make sure that when transforming the keys of a HashWithIndifferentAccess
we can still access with indifferent access in Ruby 2.5.

Closes #32007.
上级 f7ebeeed
......@@ -311,6 +311,14 @@ def transform_keys(*args, &block)
dup.tap { |hash| hash.transform_keys!(*args, &block) }
end
def transform_keys!
return enum_for(:transform_keys!) { size } unless block_given?
keys.each do |key|
self[yield(key)] = delete(key)
end
self
end
def slice(*keys)
keys.map! { |key| convert_key(key) }
self.class.new(super)
......
......@@ -404,6 +404,12 @@ def test_indifferent_transform_keys
assert_equal({ "aa" => 1, "bb" => 2 }, hash)
assert_instance_of ActiveSupport::HashWithIndifferentAccess, hash
hash = ActiveSupport::HashWithIndifferentAccess.new(@strings).transform_keys { |k| k.to_sym }
assert_equal(1, hash[:a])
assert_equal(1, hash["a"])
assert_instance_of ActiveSupport::HashWithIndifferentAccess, hash
end
def test_indifferent_transform_keys_bang
......@@ -412,6 +418,13 @@ def test_indifferent_transform_keys_bang
assert_equal({ "aa" => 1, "bb" => 2 }, indifferent_strings)
assert_instance_of ActiveSupport::HashWithIndifferentAccess, indifferent_strings
indifferent_strings = ActiveSupport::HashWithIndifferentAccess.new(@strings)
indifferent_strings.transform_keys! { |k| k.to_sym }
assert_equal(1, indifferent_strings[:a])
assert_equal(1, indifferent_strings["a"])
assert_instance_of ActiveSupport::HashWithIndifferentAccess, indifferent_strings
end
def test_indifferent_transform_values
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册