From 12600d77dd1757365dc86e6aec5cafb48ec49886 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 5 Jul 2006 17:32:16 +0000 Subject: HashWithIndifferentAccess shouldn't confuse false and nil. Closes #5601. Nor should it mistreat legitimate nil values. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4555 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../active_support/core_ext/hash/indifferent_access.rb | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'activesupport/lib/active_support/core_ext/hash/indifferent_access.rb') diff --git a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb index 9af626c8da..9a3b02e5d5 100644 --- a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb +++ b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb @@ -10,15 +10,18 @@ class HashWithIndifferentAccess < Hash super(constructor) end end - + def default(key = nil) - value = self[key.to_s] if key.is_a?(Symbol) - value ? value : super + if key.is_a?(Symbol) && include?(key = key.to_s) + self[key] + else + super + end end alias_method :regular_writer, :[]= unless method_defined?(:regular_writer) alias_method :regular_update, :update unless method_defined?(:regular_update) - + def []=(key, value) regular_writer(convert_key(key), convert_value(value)) end @@ -27,7 +30,7 @@ class HashWithIndifferentAccess < Hash other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) } self end - + alias_method :merge!, :update def key?(key) @@ -49,7 +52,7 @@ class HashWithIndifferentAccess < Hash def dup HashWithIndifferentAccess.new(self) end - + def merge(hash) self.dup.update(hash) end @@ -60,7 +63,7 @@ class HashWithIndifferentAccess < Hash def stringify_keys!; self end def symbolize_keys!; self end - + protected def convert_key(key) key.kind_of?(Symbol) ? key.to_s : key -- cgit v1.2.3