From: Martin DeMello Date: 2002-11-23T05:38:44+09:00 Subject: Re: [FAQ] Multiple constructors? Nat Pryce wrote: > On Fri, 2002-11-22 at 10:17, Martin DeMello wrote: >> Nat Pryce wrote: >> > If you want to have different constructors for different argument types, >> > define different constructors on your class. Your code will then be >> > both more flexible and more readable. E.g. If you had a class that >> > could be created with either a String and a Regexp you would define >> > constructors new_from_string(a_string) and new_from_regexp(a_regexp). >> >> This won't let me do something like >> >> my_objs = input_objs.map {|i| MyClass.new(i)} > > That's true but how often does one put arbitrary objects into a > collection and then use them to create something? And if you do, you > can easily put different types of objects into different collections. Well, in general, I think that if you have to select on type at some point in your code, it's neater to have it bundled into the constructor. But now that you mention it, a better way to code the above might be to have your class extend each object type it accepts with a method to return the appropriately initialised instance, and avoid explicit type dispatching altogether. Something like my_objs = input_objs.map {|i| i.newmyclass} as you suggested I do with the generators. martin