Skip to content

Instantly share code, notes, and snippets.

@gstark
Created January 2, 2012 16:37
Show Gist options
  • Save gstark/1551320 to your computer and use it in GitHub Desktop.
Save gstark/1551320 to your computer and use it in GitHub Desktop.
class Foo
attr_accessor :x, :y, :z
def initialize(x,y,z)
self.x = x
self.y = y
self.z = z
end
end
class Bar
attr_accessor :a, :b, :c
def initialize(a,b,c)
self.a = a
self.b = b
self.c = c
end
end
module FooComparitor
def eql?(other)
# self.hash == other.hash
if other.kind_of?(Bar)
other.a == self.x && other.b == self.y
else
super
end
end
def hash
[self.x, self.y].hash
end
end
module BarComparitor
def eql?(other)
# self.hash == other.hash
puts "Comparign #{self} to #{other}"
if other.kind_of?(Foo)
self.a == other.x && self.b == other.y
else
super
end
end
def hash
[self.a, self.b].hash
end
end
foos = [Foo.new(1,2,3),Foo.new(4,5,6),Foo.new(9,9,9)]
bars = [Bar.new(1,2,3),Bar.new(4,5,6),Bar.new(1,1,1)]
foos.each { |foo| foo.extend(FooComparitor) }
bars.each { |bar| bar.extend(BarComparitor) }
puts (foos - bars).inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment