kibleur.christophe wrote:
> Hi,
> let's say I've got a class Point2D to write, it must have 2 methods to
> initialize :
> 1. one with cartesian coordinates i.e : p = Point2D.new(x,y) given the
> numbers x and y;
> 2. a second with polar coordinates i.e : p = Point2D.new(R,theta) given
> the radius R>0 and an angle theta;
> How can I manage this ?

One way would be to take keyword arguments and figure out which you were 
given.

class Point2D
  def initialize( params )
    if params.has_key? :r and params.has_key? :theta
      ## initialize from polar
    elsif params.has_key? :x and params.has_key? :y
      ## initialize from cartesian
    else
      raise ArgumentError.new( "You must provide either :r and :theta OR 
:x and :y" )
    end
  end
end

cart = Point2D.new( :x => 42, :y => -4 )
polar = Point2D.new( :r => 3.14159, :theta => (Math::PI/4) )

-- 
Posted via http://www.ruby-forum.com/.