mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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
This commit is contained in:
parent
c51f9fdc78
commit
12600d77dd
3 changed files with 70 additions and 38 deletions
|
@ -1,5 +1,7 @@
|
|||
*SVN*
|
||||
|
||||
* HashWithIndifferentAccess shouldn't confuse false and nil. #5601 [shugo@ruby-lang.org]
|
||||
|
||||
* Fixed HashWithIndifferentAccess#default #5586 [chris@seagul.co.uk]
|
||||
|
||||
* More compatible Hash.create_from_xml. #5523 [nunemaker@gmail.com]
|
||||
|
|
|
@ -12,8 +12,11 @@ class HashWithIndifferentAccess < Hash
|
|||
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)
|
||||
|
|
|
@ -79,6 +79,34 @@ class HashExtTest < Test::Unit::TestCase
|
|||
assert_equal [1, 2], @mixed.values_at(:a, :b)
|
||||
end
|
||||
|
||||
def test_indifferent_reading
|
||||
hash = HashWithIndifferentAccess.new
|
||||
hash["a"] = 1
|
||||
hash["b"] = true
|
||||
hash["c"] = false
|
||||
hash["d"] = nil
|
||||
|
||||
assert_equal 1, hash[:a]
|
||||
assert_equal true, hash[:b]
|
||||
assert_equal false, hash[:c]
|
||||
assert_equal nil, hash[:d]
|
||||
assert_equal nil, hash[:e]
|
||||
end
|
||||
|
||||
def test_indifferent_reading_with_nonnil_default
|
||||
hash = HashWithIndifferentAccess.new(1)
|
||||
hash["a"] = 1
|
||||
hash["b"] = true
|
||||
hash["c"] = false
|
||||
hash["d"] = nil
|
||||
|
||||
assert_equal 1, hash[:a]
|
||||
assert_equal true, hash[:b]
|
||||
assert_equal false, hash[:c]
|
||||
assert_equal nil, hash[:d]
|
||||
assert_equal 1, hash[:e]
|
||||
end
|
||||
|
||||
def test_indifferent_writing
|
||||
hash = HashWithIndifferentAccess.new
|
||||
hash[:a] = 1
|
||||
|
@ -401,5 +429,4 @@ class HashToXmlTest < Test::Unit::TestCase
|
|||
hash_wia = hash.with_indifferent_access
|
||||
assert_equal 3, hash_wia.default
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in a new issue