Our goal is to make it so that we have a stack equality operator that is both a stand-in replacement for bag's equality operator and does the right thing when dealing with a stack operation. Liskov suggests that you could define something like Bag#bag_equal?
, which Stack
must expose and implement to match Bag's behavior, and that you could add a second stack_equal?
method for doing ordered comparisons. However, this feels like a kludge to me, and so I tried to take another angle, following a pattern found in Ruby's Numeric
class (i.e. its integer?
and float?
methods):
require "set"
class Bag
def ==(other)
[Set.new(data), limit] == [Set.new(other.send(:data)), other.send(:limit)]