From: Massimiliano Mirra Date: 2001-12-30T02:11:22+09:00 Subject: [ruby-talk:29636] Re: promoting self to subclass (was extending method of class A ...) On Sat, Dec 29, 2001 at 09:00:56AM +0900, Mark Hahn wrote: > How about making it: self.promote(Klass) or maybe Kernel::promote(Klass) ? > The same code that does the promotion could set the extra instance > variables. Or, even better, the initializer of the subclass could be called > with params from the promote call: self = Klass.new(*params). In general > this could be considered "assignment to self". > > Where I've run into this need is when an initializer realizes that, based on > input params, there is a more specific class for this data. Another case is > when an object is created and the "type" gets refined later. If you want a > specific example of this being used, let me know. Not sure I'm following you, but what about this: class Foo def Foo.create(changing_param, *args) case changing_param when Integer SuperFoo.new(*args) when String MegaFoo.new(*args) else Foo.new(*args) end end def initialize(a, b, c) p "Foo: #{a}, #{b}, #{c}" end end class SuperFoo < Foo def initialize(a, b, c) p "SuperFoo: #{a}, #{b}, #{c}" end end class MegaFoo < Foo def initialize(a, b, c) p "MegaFoo: #{a}, #{b}, #{c}" end end Foo.create('string', 1, 2, 3) # -> "MegaFoo: 1, 2, 3" Foo.create(999, 1, 2, 3) # -> "SuperFoo: 1, 2, 3" Foo.create([9, 8, 7], 1, 2, 3) # -> "Foo: 1, 2, 3" It might cover your first case (initializer). Massimiliano