Skip to content

Instantly share code, notes, and snippets.

@djberg96
Created January 15, 2016 18:24
Show Gist options
  • Save djberg96/84db06c46ec3e99e7452 to your computer and use it in GitHub Desktop.
Save djberg96/84db06c46ec3e99e7452 to your computer and use it in GitHub Desktop.
class Foo
include Comparable
attr_accessor :x, :y, :z
def initialize(x,y,z)
@x, @y, @z = x,y,z
end
def <=>(other)
x == other.x &&
y == other.y &&
z == other.z
end
end
f1 = Foo.new(1,2,3)
f2 = Foo.new(1,2,3)
f3 = Foo.new(2,3,4)
p f1 == f2
p f1 == f3
# compare_test.rb:21: warning: Comparable#== will no more rescue exceptions of #<=> in the next release.
# compare_test.rb:21: warning: Return nil in #<=> if the comparison is inappropriate or avoid such comparison.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment