From: Jim Menard Date: 2001-05-15T10:10:04+09:00 Subject: [ruby-talk:15128] Re: initialize overload Jim Freeze writes: > Ok, the synapsis aren't firing so well today. > How do I overload a constructor to get the effect: > > m1 = Myclass.new(some_object) > m2 = Myclass.new("text", "number") > > I cannot do: > > class Myclass > def initialize(o) > # use o > end > def initiaize(type, level) > # use type and level > end > end > > > Do I have to do something like: > > class Myclass > def initialize(a, b=nil) > if Type == a.type > #do this > else > #do that > end > end > end Yes. Alternately, you can create class methods that create objects that are named, for example, Myclass.newFromObject(some_object) and Myclass.newFromText("text", "number"). I believe this is an example of the Factory pattern. Jim -- Jim Menard, jimm@io.com, http://www.io.com/~jimm/ "Dvorak users of the world flgkd!" -- Kirsten Chevalier in rec.humor.oracle.d