class Vector
  def self.polar(r, angle=0)
    new(r*Math.cos(angle), r*Math.sin(angle))
  end

  attr_reader :x, :y

  def initialize(x, y)
    raise TypeError unless x.is_a?(Numeric) and y.is_a?(Numeric)
    @x, @y = x, y
  end

  def +(other)
    raise TypeError if self.class!= other.class
    self.class.new(@x + other.x, @y + other.y)
  end

  def -@;       self.class.new(-@x, -@y)        end
  def -(other)  self + (-other)                 end

  def *(scalar)
    raise TypeError unless scalar.is_a?(Numeric)
    self.class.new(@x * scalar, @y * scalar)
  end

  def /(scalar)
    raise TypeError unless scalar.is_a?(Numeric) and scalar.nonzero?
    self.class.new(@x / scalar, @y / scalar)
  end

  def r;        @r     ||= Math.hypot(@x, @y)   end
  def angle;    @angle ||= Math.atan2(@y, @x)   end
  def polar;    [r, angle]                      end
  def rect;     [@x, @y]                        end
  def to_s;           end
  alias inspect to_s
end

p v = Vector.new(1,1)                   
p w = Vector.new(3,4)                   
p v + w                                 
p v - w                                 
p -v                                    
p w * 5                                 
p w / 2.0                               
p w.x                                   
p w.y                                   
p v.polar                               
p w.polar                               
p z = Vector.polar(1, Math::PI/2)       
p z.rect                                
p z.polar                               
p z = Vector.polar(-2, Math::PI/4)      
p z.polar