class Array
def recursive_diff(other, self_key = 'self', other_key = 'other')
-
(Array)-
def recursive_diff(other, self_key = 'self', other_key = 'other') if other.is_a?(Array) new_objects_self = (self - other) new_objects_other = (other - self) unmatched_objects_self = [] array_result = [] # Try to match objects to reduce noise new_objects_self.each do |value| if value.is_a?(Hash) other_value = new_objects_other.find do |other| other.is_a?(Hash) && (value['displayName'] == other['displayName']) end if other_value new_objects_other.delete(other_value) match_diff = value.recursive_diff(other_value, self_key, other_key) array_result << { value['displayName'] => match_diff} unless match_diff == {} else unmatched_objects_self << value end end end unless unmatched_objects_self.empty? array_result << { self_key => unmatched_objects_self.map do |v| { v['displayName'] => v } end } end unless new_objects_other.empty? array_result << { other_key => new_objects_other.map do |v| { v['displayName'] => v } end } end array_result unless array_result == [] else super end end