From: Joel VanderWerf Date: 2004-04-26T04:40:22+09:00 Subject: Re: How do I get multiple constructors? Sam Roberts wrote: > I'm aware of the case where you have a few constructors, but they are > implemented as some kind of "syntax sugar" on top of one constructor > that does all the work (the example from "Singletons and other > Constructors" in Pickaxe chapter 3). > > I have two *different* ways of creating an object, and I can't write > them all in terms of one single constructor. Also, I can't write > them in terms of public methods of the object (they set up internal > things that can't be changed by the public). > > I thought I could do this: You could make them private and use #send to access them from your constructors. > class Foo > private_class_method :new > > # private :create > # private :decode > > def Foo.create(*args) new.send :create, *args > end > > def Foo.decode(*args) new.send :decode, *args > end > > def create(a, b) > puts "create(#{a},#{b})" > self > end > > def decode(a) > puts "decode(#{a})" > self > end private :create private :decode > end > > Foo.create(1,2) > Foo.decode(3) > > Foo.decode(4).decode(5) # Should NOT work! Now, the last line gives an error.