From: mental@... Date: 2005-12-10T06:48:22+09:00 Subject: Re: different methods for initializing one object? Quoting Mike Fletcher : > 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 ? If you are always storing cartesian coordinates internally, I'd suggest introducing a different factory method for creating points from polar coordinates. For example: class Point2D attr_accessor :x, :y def initialize( x, y ) @x, @y = x, y end def self.new_polar( r, theta ) new( r * Math::cos( theta ), r * Math::sin( theta ) ) end end examples: cart = Point2D.new( 42, -4 ) polar = Point2D.new_polar( 3.14159, Math::PI / 4 ) -mental