Skip to content

Instantly share code, notes, and snippets.

@pinzolo
Last active October 27, 2015 02:41
Show Gist options
  • Save pinzolo/a2397739a8b5bfb0c4c5 to your computer and use it in GitHub Desktop.
Save pinzolo/a2397739a8b5bfb0c4c5 to your computer and use it in GitHub Desktop.
class Point
attr_reader :x, :y
def initialize(x, y)
@x = x
@y = y
end
def ==(other)
@x == other.x && @y == other.y
end
def to_s
"(#{x}, #{y})"
end
def inspect
to_s
end
end
p [Point.new(1, 1), Point.new(2, 2), Point.new(3, 3)] - [Point.new(3, 3), Point.new(1, 1)]
# => [(1, 1), (2, 2), (3, 3)]
class Point
attr_reader :x, :y
def initialize(x, y)
@x = x
@y = y
end
def ==(other)
eql?(other)
end
def eql?(other)
@x == other.x && @y == other.y
end
def hash
@x + @y
end
def to_s
"(#{x}, #{y})"
end
def inspect
to_s
end
end
p [Point.new(1, 1), Point.new(2, 2), Point.new(3, 3)] - [Point.new(3, 3), Point.new(1, 1)]
# => [(2, 2)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment