module HashDiff

Constants

VERSION

Public Class Methods

diff(a,b) click to toggle source
# File lib/hash_diff.rb, line 6
def self.diff(a,b)
  return nil if a == b

  diff = {}
  keys = Set.new(a.keys + b.keys)
  keys.each do |key|
    a_val = get(a, key)
    b_val = get(b, key)
    if a[key].is_a?(Enumerable) || b[key].is_a?(Enumerable)
      diff[key] = diff(a_val, b_val)
    end

    if a_val != b_val
      diff[key] = [a_val, b_val]
    end
  end

  diff unless diff.empty?
end

Private Class Methods

get(hash, key) click to toggle source
# File lib/hash_diff.rb, line 27
def self.get(hash, key)
  hash[key.to_sym] || hash[key.to_s]
end