liufengyun / hashdiff

Hashdiff is a ruby library to to compute the smallest difference between two hashes
MIT License
560 stars 63 forks source link

==/equal? overrides not used when checking keys #85

Open renaudpr opened 1 year ago

renaudpr commented 1 year ago

Hello, The override of the ==/equal? methods seem to be used when checking values of an Hash, but not when checking the keys. Here is an example with a simple class :

class Color
  attr_reader :value

  def initialize(value)
    @value = value
  end

  def equal?(other)
    return false unless other.is_a?(Color)

    @value == other.value
  end

  def ==(other)
    return false unless other.is_a?(Color)

    @value == other.value
  end
end

Hashdiff.best_diff({1 => Color.new('red')}, {1 => Color.new('red')}) returns [] as expected but Hashdiff.best_diff({Color.new('red') => 1}, {Color.new('red') => 1}) returns [["~", "#<Color:0x000014ac0e49d418>", 1, nil]]

Thanks!