changing class at runtime

R

Ryan Paul

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
 
H

Hal Fulton

Ryan said:
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
 
M

Mauricio Fernández

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
 
R

Ryan Paul

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

gabriele renzi

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>

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 ?
 
R

Ryan Paul

<snip>

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 ?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top