From: Szabolcs Szasz Date: 2002-10-20T11:28:27+09:00 Subject: Re: Is subclass known in superclass.initialize? (virtual constructor theme...) I did start with the class/type method, but still only saw the superclass in the constructor... Strange, I must have missed something subtle in the actual code (which is different from my gutted example below). Needless to say, now it does work just fine in the original code, too... Well, it can be a bit frustrating to be a newbie sometimes... ;) Thanks very much, Sab ----- Original Message ----- From: "Dave Thomas" To: "ruby-talk ML" Sent: Sunday, October 20, 2002 4:07 AM Subject: Re: Is subclass known in superclass.initialize? (virtual constructor theme...) > "Szabolcs Szasz" writes: > > > Is there a reasonable replacement for '?' below? > > > > class Super > > def initialize > > sub = '?' # something that yields obj.class > > puts "Created for subclass #{sub}" > > end > > end > > 'type' returns the class of its receiver, so: > > class Super > def initialize > sub = type > puts "Created for subclass #{sub}" > end > end > > class Sub < Super > end > > obj = Super.new #=> Created for subclass Super > obj = Sub.new #=> Created for subclass Sub > > > So can also use #class, but you have to be careful to call it with an > explicit receiver, or Ruby will think you're starting a class > definition. > > > Cheers > > > Dave > >