From: Austin Ziegler Date: 2002-11-22T02:06:11+09:00 Subject: Re: [FAQ] Multiple constructors? On Fri, 22 Nov 2002 01:01:24 +0900, Nat Pryce wrote: > On Thu, 2002-11-21 at 15:27, Gavin Sinclair wrote: >> If you need to branch on type, you can do this >> >> def initialize(value) >> case value >> when String then ... >> when Regexp then ... >> else ... >> end > I would avoid this because it is not branching on type, but > instead branching on how an object *implements* its type, which > should be of no concern to other objects. [...] > 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). Mmmm. No. That unnecessarily exposes producer implementation details to the consumer class. If the behaviours are what are important, then the producer class should use #respond_to? and not the type. In my cases where I respond to multiple *types*, I have done so because I explicitly care about the types used because my code responds slightly differently to different types in constructors, but only internally. It should never matter to the consumer that the responses are internally different -- only that the outputs are the same. -austin -- Austin Ziegler, austin@halostatue.ca on 2002.11.21 at 12.01.45