Changing class at runtime

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

I figure that even if the solution turns out to be devoid of elegance, I
can toss it into a method of the ‘Class’ class to achieve what I want.

TIA
–SegPhault

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:

[snip]

You can’t do it in Ruby unless you use Mauricio’s “evil” library. (He
named it, I didn’t) which has a #become method, a la Smalltalk.

If it really is just one method you’re concerned with, you could always
redefine it. Ruby won’t let you change the class of an object, but it
will let you arbitrarily add/change/delete stuff in the object.

Hal

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

···

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


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

Can I dare to ask why you need this?
I discovere stuff like runtime change of class and #become just with
the Evil library, then found that this was possible in other
languages.
But I wonder what is this useful for ?

I don’t have a clue on what you’re doing but could not you just extend
the object at runtime with some mixins ?

···

il Sat, 15 May 2004 07:47:03 GMT, Ryan Paul segphault@sbcglobal.net ha scritto::

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:

Very cool! Thanks!! In addition to be exactly what I wanted, Its also a
beautiful example of some advanced ruby techniques.

···

On Sat, 15 May 2004 17:03:15 +0900, Mauricio Fernández wrote:

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

At the moment, I have no actual reason for wanting to do this in ruby. I
have used it several times in python, mainly to work around problems in
poorly designed oo libraries. I imagine that ruby’s superior support for
class extension, and a few other more graceful facets of the language
nullify the necessity of hacks like this, but its nice to know that I can
do it if I ever need to. Honestly, I asked for the sake of finding out if
ruby could do it, more than for the sake of actually being able to. I like
to keep options open, and being able to manipulate the class like that
gives me yet another way to solve a potential problem in the future.
Incidentally, perusing the content of the ‘evil’ library has provided me
with some fascinating insights about some of the advanced features of the
language, so in that respect, it has already proven itself to be valuable.

···

On Sat, 15 May 2004 09:48:02 +0000, gabriele renzi wrote:

il Sat, 15 May 2004 07:47:03 GMT, Ryan Paul segphault@sbcglobal.net > ha scritto::

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:

Can I dare to ask why you need this?
I discovere stuff like runtime change of class and #become just with
the Evil library, then found that this was possible in other
languages.
But I wonder what is this useful for ?

I don’t have a clue on what you’re doing but could not you just extend
the object at runtime with some mixins ?