Skip to content

Instantly share code, notes, and snippets.

@makaroni4
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makaroni4/264628781e0f57d06019 to your computer and use it in GitHub Desktop.
Save makaroni4/264628781e0f57d06019 to your computer and use it in GitHub Desktop.
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