From: Dave Thomas Date: 2000-12-14T08:09:14+09:00 Subject: [ruby-talk:7178] Re: Multiple constructors. "Conrad Schneiker" writes: > Someone asked me the following question. Is there some clever way to deal > with this? > > # When creating a class, is "initialize" the only form of constructor? The answer is yes and no. Eventually, the only way to create an object is by calling .new, which in turn calls .initialize. So the initialize method ends up doing the initialization of a constructed object. However, you can have any number of class methods that _act_ as constructors, and which then invoke .new internally. In fact, some classes even hide .new, so that you're forced to use these alternative methods. As a simple example (and one which is in very, very bad OO style, so no flames please) you could have: class Rectangle def initialize(width, height) @width, @height = width, height end def Rectangle.square(side) Rectangle.new(side, side) end end This would effectively give you two constructors r = Rectangle.new(2, 3) s = Rectangle.square(4) Many of Ruby's built in classes to this (File.new vs. File.open, for example). Regards Dave