24 September 2007 Manfred Stienstra and Norbert Crombach

The Case Equality, Treequal or Equaal operator, however you want to call it, allows us to match objects in a more meaningful manner than plain equality. In this episode Manfred shows how you can override the method on your own classes to do advanced matching. Please note that, although Manfred thinks he’s implementing the disjoint set of markers, he’s actually implementing the union.

Download episode

class Marker
  attr_accessor :expression

  def initialize(e)
    @expression = e
  end

  def ===(o)
    to_a.any? { |e| e === o }
  end

  def |(o)
    Marker.new to_a + o.to_a
  end

  def to_a
    [expression].flatten
  end
end