From: Dave Thomas Date: 2002-10-20T11:07:51+09:00 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