New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an optional block to HashWithIndifferentAccess#update and #merge #7519
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,10 +95,10 @@ def []=(key, value) | |
|
||
alias_method :store, :[]= | ||
|
||
# Updates the receiver in-place merging in the hash passed as argument: | ||
# Updates the receiver in-place, merging in the hash passed as argument: | ||
# | ||
# hash_1 = ActiveSupport::HashWithIndifferentAccess.new | ||
# hash_2[:key] = "value" | ||
# hash_1[:key] = "value" | ||
# | ||
# hash_2 = ActiveSupport::HashWithIndifferentAccess.new | ||
# hash_2[:key] = "New Value!" | ||
|
@@ -110,12 +110,27 @@ def []=(key, value) | |
# 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 was is unespecified. | ||
# 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} | ||
# | ||
def update(other_hash) | ||
if other_hash.is_a? HashWithIndifferentAccess | ||
super(other_hash) | ||
else | ||
other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) } | ||
other_hash.each_pair do |key, value| | ||
if block_given? && key?(key) | ||
value = yield(convert_key(key), self[key], value) | ||
end | ||
regular_writer(convert_key(key), convert_value(value)) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could probably cache the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I did consider it but a) it will make the |
||
self | ||
end | ||
end | ||
|
@@ -173,8 +188,8 @@ def dup | |
# This method has the same semantics of +update+, except it does not | ||
# modify the receiver but rather returns a new hash with indifferent | ||
# access with the result of the merge. | ||
def merge(hash) | ||
self.dup.update(hash) | ||
def merge(hash, &block) | ||
self.dup.update(hash, &block) | ||
end | ||
|
||
# Like +merge+ but the other way around: Merges the receiver into the | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you put a space after the
{
and before the}
. Like this{ "key" => 22 }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While that's the amount of whitespace I would use in my own code when declaring a hash literal, the snippet you're referring to is intended to represent the value of the expression preceding it (note the
# =>
). Omitting surrounding whitespace is consistent with what IRB would print out, and indeed with the syntax used throughout the rest of the file: see lines 30, 106, and 200.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. Thanks