blob: b904f49fa87ca571eee7a54cea1fb00dd70ff8ab (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class Hash
# Returns a hash that represents the difference between two hashes.
#
# Examples:
#
# {1 => 2}.diff(1 => 2) # => {}
# {1 => 2}.diff(1 => 3) # => {1 => 2}
# {}.diff(1 => 2) # => {1 => 2}
# {1 => 2, 3 => 4}.diff(1 => 2) # => {3 => 4}
def diff(h2)
dup.delete_if { |k, v| h2[k] == v }.merge!(h2.dup.delete_if { |k, v| has_key?(k) })
end
end
|