Last active
August 29, 2015 14:04
-
-
Save makaroni4/264628781e0f57d06019 to your computer and use it in GitHub Desktop.
This file contains 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
def smart_subtract(arr_1, arr_2, action) | |
hash_1 = Hash[arr_1] | |
hash_2 = Hash[arr_2] | |
result = [] | |
hash_1.each do |key, value| | |
if hash_2[key] | |
result << [key, value.send(action, hash_2[key])] | |
else | |
result << [key, value] | |
end | |
end | |
result.to_a | |
end | |
arr_1 = [[33, 2], [83, 1], [21, 1]] | |
arr_2 = [[83, 1], [33,1]] | |
p smart_subtract(arr_1, arr_2, "-") | |
# [[33, 1], [83, 0], [21, 1]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment