Created
August 29, 2012 15:58
-
-
Save aaronbbrown/3514818 to your computer and use it in GitHub Desktop.
Subtract Hashes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'pp' | |
class Hash | |
def subtract (h) | |
result = {} | |
self.each do |k,v| | |
result[k] = (self[k] - h[k]) if h[k] | |
end | |
result | |
end | |
end | |
h1 = { 'a' => 5, | |
'b' => 10, | |
'c' => 15 } | |
h2 = { 'a' => 20, | |
'b' => 30, | |
'c' => 40 } | |
pp h2.subtract(h1) | |
# -> {"a"=>15, "b"=>20, "c"=>25} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment