Skip to content

Instantly share code, notes, and snippets.

@bjeanes
Created April 10, 2009 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjeanes/92968 to your computer and use it in GitHub Desktop.
Save bjeanes/92968 to your computer and use it in GitHub Desktop.
class Point
attr_reader :x, :y
def initialize(x, y)
@x, @y = x, y
end
def ~ point
Math.sqrt((point.x - self.x) ** 2 +
(point.y - self.y) ** 2)
end
end
p1,p2 = Point.new(-4,14), Point.new(12,-34)
puts (p1.~(p2))
# puts (p1 ~ p2) # ArgumentError: wrong number of arguments (0 for 1)
# Treating p1 ~ p2 as p1(~p2) instead of p1.~(p2)
# Seems inconsistent with behaviour of +@ an -@
class Huh
def ~@
puts "a"
end
def ~
puts "b"
end
end
~Huh.new # => "b"
# Why? shouldn't this call Huh#~@ and leave Huh#~ for something link h1 ~ h2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment