From: Piers Cawley Date: 2001-08-12T17:02:04+09:00 Subject: [ruby-talk:19584] Re: Perl/Python/Ruby common backend (Parrot, can Ruby play too?) Harry Ohlsen writes: > Ned Konz wrote: > > >> OTOH, Ruby *doesn't* allow to change an object's class, unlike Perl and > >> Python. The exception to that is when a object has class=Foo and > >> changes to class=(new singleton class inheriting from Foo). > > > > You know, I've been meaning to ask how to do this (change the class of an > > object). Is there something akin to Smalltalk's "become:" in which two > > objects swap identities? That's one way to do this, of course. > > > > Also, how can one change the superclass of an existing class in Ruby? > > My apologies if I missed an explanation earlier in this thread, but ... > > Can someone explain under what circumstances one would want to change the > class of an object? An example of the idiom would be useful. Consider a set of 'State' classes. State objects all have the same set of instance variables. Now, if StateA decides that the next state should be StateB then (in perlish syntax), it would do sub handler { my $self = shift; ... bless $self, 'StateB'; return; } And then your state machine loop would simply look like: while ($state->halted) { $state->handler } And you let the language handle all the icky state tables. I really must go an implement something 'real' that works this way... -- Piers Cawley www.iterative-software.com