Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1099 from dlee/optimize_indifferent_access
Optimize parts of HashWithIndifferentAccess
  • Loading branch information
josevalim committed May 17, 2011
2 parents a52dc0d + 94617d7 commit ab9639f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 10 additions & 2 deletions activesupport/lib/active_support/hash_with_indifferent_access.rb
Expand Up @@ -10,6 +10,10 @@ def extractable_options?
true
end

def with_indifferent_access
self
end

def initialize(constructor = {})
if constructor.is_a?(Hash)
super()
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions activesupport/test/core_ext/hash_ext_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit ab9639f

Please sign in to comment.