rails/rails — pull request #7519 by leocassarani (2012-09-04 09:36:15+00:00)
Title: Add an optional block to HashWithIndifferentAccess#update and #merge

When a block is passed into `HashWithIndifferentAccess#update` or `#merge`, it will be invoked for each duplicated key with the key in question, the value for the key in the receiver, and the value in the hash being merged. The return value of the block will be used to resolve the conflict:

hash_1 = ActiveSupport::HashWithIndifferentAccess.new
hash_1[:key] = 10

hash_2 = ActiveSupport::HashWithIndifferentAccess.new
hash_2['key'] = 20

hash_1.merge(hash_2) { |key, old, new| old + new } # => {"key"=>30}

This is consistent with long-standing behaviour of the Standard Library's `Hash` class. Indeed, without this change, it would not be immediately obvious to someone passing a block into `HashWithIndifferentAccess#merge` that its behaviour doesn't reflect `Hash#merge`, as the block will be silently ignored.