mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make HashWithIndifferentAccess#update behave like Hash#update by returning the hash. Closes #3419, #3425
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3388 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
b30ccefe6f
commit
36fc181a57
3 changed files with 30 additions and 2 deletions
|
@ -1,5 +1,7 @@
|
|||
*SVN*
|
||||
|
||||
* Make HashWithIndifferentAccess#update behave like Hash#update by returning the hash. #3419, #3425 [asnem@student.ethz.ch, JanPrill@blauton.de, Marcel Molina Jr.]
|
||||
|
||||
* Add ActiveSupport::JSON and Object#to_json for converting Ruby objects to JSON strings. [Sam Stephenson]
|
||||
|
||||
* Add Object#with_options for DRYing up multiple calls to methods having shared options. [Sam Stephenson] Example:
|
||||
|
|
|
@ -18,9 +18,12 @@ class HashWithIndifferentAccess < Hash
|
|||
def []=(key, value)
|
||||
regular_writer(convert_key(key), convert_value(value))
|
||||
end
|
||||
def update(hash)
|
||||
hash.each {|key, value| self[key] = value}
|
||||
|
||||
def update(other_hash)
|
||||
other_hash.each {|key, value| self[key] = value}
|
||||
self
|
||||
end
|
||||
alias_method :merge!, :update
|
||||
|
||||
def key?(key)
|
||||
super(convert_key(key))
|
||||
|
|
|
@ -93,6 +93,29 @@ class HashExtTest < Test::Unit::TestCase
|
|||
assert_equal hash[3], 3
|
||||
end
|
||||
|
||||
def test_indifferent_update
|
||||
hash = HashWithIndifferentAccess.new
|
||||
hash[:a] = 'a'
|
||||
hash['b'] = 'b'
|
||||
|
||||
updated_with_strings = hash.update(@strings)
|
||||
updated_with_symbols = hash.update(@symbols)
|
||||
updated_with_mixed = hash.update(@mixed)
|
||||
|
||||
assert_equal updated_with_strings[:a], 1
|
||||
assert_equal updated_with_strings['a'], 1
|
||||
assert_equal updated_with_strings['b'], 2
|
||||
|
||||
assert_equal updated_with_symbols[:a], 1
|
||||
assert_equal updated_with_symbols['b'], 2
|
||||
assert_equal updated_with_symbols[:b], 2
|
||||
|
||||
assert_equal updated_with_mixed[:a], 1
|
||||
assert_equal updated_with_mixed['b'], 2
|
||||
|
||||
assert [updated_with_strings, updated_with_symbols, updated_with_mixed].all? {|hash| hash.keys.size == 2}
|
||||
end
|
||||
|
||||
def test_assert_valid_keys
|
||||
assert_nothing_raised do
|
||||
{ :failure => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ])
|
||||
|
|
Loading…
Reference in a new issue