From: "Mauricio Fernández" Date: 2004-05-15T17:03:15+09:00 Subject: Re: changing class at runtime On Sat, May 15, 2004 at 04:48:54PM +0900, Ryan Paul wrote: > I'm trying to find a way to arbitrarily alter the class of an instance at > runtime. The documentation didn't appear to have any clues. > > Ultimately, I want to be able to do something like this: > > class MyClass1 > def fnc1 > puts "fnc1 in class 1" > end > end > > class MyClass2 > def fnc1 > puts "fnc1 in class 2" > end > end > > a = MyClass1.new > a.fnc1 > puts a.class > > a.class = MyClass2 > a.fnc2 > puts a.class > > Python lets you do it thusly: someinstance.__class__ = someclass batsman@tux-chan:/tmp$ cat ghfgkhjioujklg.rb require 'evil' class MyClass1 def fnc1 puts "fnc1 in class 1" end end class MyClass2 def fnc1 puts "fnc1 in class 2" end end a = MyClass1.new a.fnc1 puts a.class a.class = MyClass2 a.fnc1 puts a.class batsman@tux-chan:/tmp$ ruby ghfgkhjioujklg.rb /home/batsman/usr/lib/ruby/1.8/dl/import.rb:12: warning: instance variable @types not initialized fnc1 in class 1 MyClass1 fnc1 in class 2 MyClass2 http://evil.rubyforge.org -- Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com Q: What's the big deal about rm, I have been deleting stuff for years? And never lost anything.. oops! A: ... -- From the Frequently Unasked Questions