Permalink
Browse files

Merge pull request #1099 from dlee/optimize_indifferent_access

Optimize parts of HashWithIndifferentAccess
  • Loading branch information...
2 parents a52dc0d + 94617d7 commit ab9639f945b1f5de40b2889ce47953dad645bdf5 @josevalim josevalim committed May 17, 2011
@@ -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

0 comments on commit ab9639f

Please sign in to comment.