Created
October 20, 2015 14:21
-
-
Save duderman/d96f75ce3c814e6d514c 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
class Hash | |
def deep_diff(other) | |
(self.keys + other.keys).uniq.inject({}) do |memo, key| | |
left = self[key] | |
right = other[key] | |
next memo if left == right | |
if left.respond_to?(:deep_diff) && right.respond_to?(:deep_diff) | |
memo[key] = left.deep_diff(right) | |
else | |
memo[key] = [left, right] | |
end | |
memo | |
end | |
end | |
end | |
class Array | |
def deep_diff(array) | |
largest = [self.count, array.count].max | |
memo = {} | |
0.upto(largest - 1) do |index| | |
left = self[index] | |
right = array[index] | |
next if left == right | |
if left.respond_to?(:deep_diff) && right.respond_to?(:deep_diff) | |
memo[index] = left.deep_diff(right) | |
else | |
memo[index] = [left, right] | |
end | |
end | |
memo | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment