运算符重载

class Point
    attr_accessor :x, :y

    def initialize(x, y)
        @x, @y = x, y
    end

    def +(other)
        @x = @x + other.x
        @y = @y + other.y
        return Point.new(@x, @y)
    end
end

p1 = Point.new(11)
p2 = Point.new(22)
p3 = p1 + p2
# p3.x is 3 and p3.y is 3
puts "(x, y) is (#{p3.x}#{p3.y})"

p3 += Point.new(35)
# p3.x is 6 and p3.y is 8
puts "(x, y) is (#{p3.x}#{p3.y})"