R
rolo
Hi
obj.class = MyClass
Is it harmful?
rolo
obj.class = MyClass
Is it harmful?
rolo
Hi
obj.class = MyClass
Is it harmful?
--- rolo said:Hi
obj.class = MyClass
Is it harmful?
rolo
=20
irb(main):002:0> {}.class =3D Array
NoMethodError: undefined method `class=3D' for {}:Hash
from (irb):2
irb(main):003:0>
Sean said:Even if there was a #class= for objects, it ranks up there with goto in
limited usefulness. Yes, it can be useful, but if you're using it,
you're probably being too clever.
I knew it was evil, I think it should be norm.
PS I know it is not allowed, but is it so for tech reasons?
Sean said:Good question. I am personally having trouble thinking of a good reason why a
programmer couldn't change the class of an object. Except for possible
problems when a different set of methods suddenly gets a set of instance
variables it might not expect (buyer beware), it seems like we ought to be
able to do that. I don't believe there's any solid technical block to doing
this, is there?
Sean O'Dell
I can't possibly imagine it being useful to do this except when the
objects have very similar, if not identical, instance variables.
That's why there are the _generic functions_ CHANGE-CLASS andThat's exactly the problem -- by forcing one object into another
class, you require that one or both classes have sufficient knowledge
of the internal data representation of the other to be able to safely
make the switch. That kind of low-level manipulation usually stays
within a class for a reason.
Another reason would be that it might be useful to change the object _inThe only real reason I can see to dynamically change the class of an
object would be in languages where there are static type constraints
that have to be satisfied. As it is, in Ruby, you can always just wrap
the original object in a proxy, or access it through an adapter that
supports all the public methods of the new class; it fits the "duck
typing" rule, without breaking encapsulation.
Scott Rubin said:
The "standard" application for the self.class= method (aka become(class))
is use it to do demand on load style proxies. Suppose you have a largish
object in external storage (database, file, whatever). You create a
small, efficient proxy item for the large object until you really need the
true data in the object. Then you load the object and let the proxy
"become" the newly loaded object.
Without class=/become methods,
(a) the proxy remains and forwards all messages to the real object, or
(b) you replace every reference in the program to the proxy with
a reference to the real object.
(a) is a small, but anoying constant cost, and (b) may be difficult,
depending on how wide spread the proxy was used.
That's why class=/become would be attractive, despite its technical
difficulties and mildly worrisome semantices.
S> variables it might not expect (buyer beware), it seems like we ought to
be S> able to do that. I don't believe there's any solid technical block
to doing S> this, is there?
If you think that the possiblity to crash ruby is not a technical reason,
then you are right it don't exist solid technical reason.
That's why class=/become would be attractive, despite its technical
difficulties and mildly worrisome semantices.
You're wrong on that. It wouldn't crash Ruby to change the class of an
object. Objects already change their class when you turn them into
singletons.
Changing it to any class of your choosing would be even simpler,
since creating a singleton involves inheriting all the old class
methods;
changing a class wouldn't require all that work.
Another reason would be that it might be useful to change the object _in
place_. That is, all existing references suddenly point to the new object.
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.