From: Nicholas Van Weerdenburg Date: 2005-01-26T01:24:21+09:00 Subject: Re: Self and Ruby Comparisons On Tue, 25 Jan 2005 19:30:54 +0900, Robert Klemme wrote: > > "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 > > >> class Foo > >> attr_accessor :name, :val > >> end > => nil > >> class Bar > >> attr_accessor :name, :val > >> end > => nil > >> f=Foo.new > => # > >> f.name="nnn" > => "nnn" > >> f.val="vvv" > => "vvv" > >> b=f.cast Bar > => # > >> b.class > => Bar > > > So the only difference is that in Ruby you can't do full prototype-based > > programming using just 100%-Ruby. > > Do you have a link or explanation that demonstrates the merits of the > capability to change an instance's class at runtime? Currently I don't > have a clue in which situations I would want this. Thx! > > Kind regards > > robert > > How about migrating legacy objects to new objects at run-time? Nick -- Nicholas Van Weerdenburg