instance method
update
Ruby on Rails 6.1.7.10
Since v3.0.20Signature
update(*other_hashes, &block)
Updates the receiver in-place, merging in the hashes passed as arguments:
hash_1 = ActiveSupport::HashWithIndifferentAccess.new hash_1[:key] = 'value' hash_2 = ActiveSupport::HashWithIndifferentAccess.new hash_2[:key] = 'New Value!' hash_1.update(hash_2) # => {"key"=>"New Value!"} hash = ActiveSupport::HashWithIndifferentAccess.new hash.update({ "a" => 1 }, { "b" => 2 }) # => { "a" => 1, "b" => 2 }
The arguments can be either an ActiveSupport::HashWithIndifferentAccess or a regular Hash. In either case the merge respects the semantics of indifferent access.
If the argument is a regular hash with keys :key and "key" only one of the values end up in the receiver, but which one is unspecified.
When given a block, the value for duplicated keys will be determined by the result of invoking the block with the duplicated key, the value in the receiver, and the value in other_hash. The rules for duplicated keys follow the semantics of indifferent access:
hash_1[:key] = 10 hash_2['key'] = 12 hash_1.update(hash_2) { |key, old, new| old + new } # => {"key"=>22}
Parameters
-
other_hashesrest -
blockblock
Source
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 128
def update(*other_hashes, &block)
if other_hashes.size == 1
update_with_single_argument(other_hashes.first, block)
else
other_hashes.each do |other_hash|
update_with_single_argument(other_hash, block)
end
end
self
end
Defined in activesupport/lib/active_support/hash_with_indifferent_access.rb line 128
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveSupport::HashWithIndifferentAccess