"Mathieu Bouchard" <matju / sympatico.ca> 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 <mystifier / users.berlios.de> 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
=> #<Foo:0x100c4220>
>> f.name="nnn"
=> "nnn"
>> f.val="vvv"
=> "vvv"
>> b=f.cast Bar
=> #<Bar:0x101a7238 @val="vvv", @name="nnn">
>> 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