From: Robert Klemme Date: 2005-01-26T01:35:48+09:00 Subject: Re: Self and Ruby Comparisons "Robert Klemme" schrieb im Newsbeitrag news:35mlboF4m5ql5U1@individual.net... > > "Mathieu Bouchard" schrieb im Newsbeitrag > news:Pine.LNX.4.21.0501250404130.16754-100000@mondrian.artengine.ca... > > > > On Mon, 24 Jan 2005, Csaba Henk wrote: > > > > > On 2005-01-23, Mystifier wrote: > > > Btw, I hope you know that Ruby has almost full support for prototype > > > based programming. > > > > Actually, Perl has it better, because you can change the class of an > > object, and you can change the list of superclasses of any class. > > > > However, this is only a slight advantage, as some other people have > > demonstrated you can do the same for Ruby using not that many lines of C > > code. > > And if a copy is sufficient you can do this even in pure Ruby: > > class Object > def cast(target_class) > copy = target_class.allocate > > instance_variables.each do |var| > copy.instance_variable_set(var, instance_variable_get(var)) > end > > copy > end > end A slightly more sophisticated version: class Object def cast(target_class) copy = case target_class when Class; target_class.allocate when Module; dup.extend(target_class) else target_class.class.allocate end instance_variables.each do |var| copy.instance_variable_set(var, instance_variable_get(var)) end copy end end Cheers robert