From: Dave Thomas Date: 2003-06-12T14:29:49+09:00 Subject: Re: Multiple Initialize methods? Nick wrote: > Hi everybody, > > Thanks for the replies. I appreciate Ruby isnt C++, or C# or Delphi, or ... > but typically you can have more than one constructor in those languages - > because typically people have requested the functionality from the language > because they help. I think I will reluctantly go with the setting default > values for the params. Its not nice in my view, but it will do. I haven't followed this thread, but has anyone suggested construction methods (sometimes called factory methods?). Make the constructor private, and use class methods to construct specific objects: class Square def Square.with_area(area) new(Math.sqrt(area)) end def Square.with_diagonal(diag) new(diag/Math.sqrt(2)) end def Square.with_side(side) new(side) end private_class_method :new def initialize(side) @side = side end end s1 = Square.with_area(4) s2 = Square.with_diagonal(2.828427) s3 = Square.with_side(2) The joy of this approach is that not everything has to be done in the constructor: because no one else can call it, the constructor can create a partially initialized object, leaving it up to your construction methods to complete the job. Cheers Dave