# -*- ruby -*-

class Point
  attr_accessor :x

  def translate(x)
    p = clone
    p.x += x
    p
  end
end


class Point2d < Point
  attr_accessor :y

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

  def length()
    Math.sqrt(@x * @x + @y * @y)
  end

  def to_s()
      "[%g, %g]" % [@x, @y]
  end
end

class Point3d < Point
  attr_accessor :y, :z

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

  def length()
    Math.sqrt(@x * @x + @y * @y + @z * @z)
  end

  def to_s()
      "[%g, %g, %g]" % [@x, @y, @z]
  end
end

p2d = Point2d.new(3, 4)
p3d = Point3d.new(1, 2, 2)

l = [ p2d, p3d ]

def test(p)
  return p.length, p.translate(1)
end

print [ test(p2d), test(p3d), l.map{|p| p.length} ].join(" "), "\n"

# after translate, we still have a Point3d
print p3d.translate(1).z, "\n"
