From: Gregory Seidman Date: 2007-03-21T10:25:20+09:00 Subject: Re: does Ruby not support multiple "initialize" methods for a class??? On Wed, Mar 21, 2007 at 10:09:36AM +0900, Chad Perrin wrote: > On Wed, Mar 21, 2007 at 08:32:37AM +0900, johan556@gmail.com wrote: > > On 3/20/07, Greg Hauptmann wrote: > > >Hi, > > > > > >Q1 - Does Ruby not support multiple "initialize" methods for a class? > > > > > >Q2 - If no is there a reason for this out of curiosity? > > > > > >Q3 - What approach is recommended if this constraint does exist? > > > > > > > I have been using the following pattern to implement multiple > > constructors for a class. It is only a variation of the already > > proposed solutions, but by using "instance_eval" I could avoid having > > to create separate new/initialize-like methods for each constructor. > > I've heard it said -- and I tend to agree -- that regular use of > "patterns" is a sign that the language lacks something. I wonder if > that is the case with Ruby, as applies to multiple constructor behavior > implementation. > > I say "wonder" because I really am not certain in this case. Well, it's a choice. You can either have overloading or dynamic typing, but not both. The "pattern" is just a matter of having different names for your constructors. The simple, common case of a single constructor is supported by the new/initialize separation, but multiple constructors are still pretty easy. Note that Objective-C, another Smalltalk-like, dynamically-typed language, uses the same pattern. > CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ] --Greg