提交 94617d7d 编写于 作者: D David Lee

Optimize parts of HashWithIndifferentAccess

上级 b0385fe1
......@@ -10,6 +10,10 @@ def extractable_options?
true
end
def with_indifferent_access
self
end
def initialize(constructor = {})
if constructor.is_a?(Hash)
super()
......@@ -58,8 +62,12 @@ def []=(key, value)
# hash_1.update(hash_2) # => {"key"=>"New Value!"}
#
def update(other_hash)
other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
self
if other_hash.is_a? HashWithIndifferentAccess
super(other_hash)
else
other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
self
end
end
alias_method :merge!, :update
......
......@@ -971,6 +971,11 @@ def test_should_nil_if_no_default_value_is_supplied
assert_nil hash_wia.default
end
def test_should_return_self_for_with_indifferent_access
hash_wia = HashWithIndifferentAccess.new
assert_equal hash_wia, hash_wia.with_indifferent_access
end
def test_should_copy_the_default_value_when_converting_to_hash_with_indifferent_access
hash = Hash.new(3)
hash_wia = hash.with_indifferent_access
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册