Does someone know if it’s possible to dynamically switch superclass, or
change the ancestor chain on an already existing class? (Or if it’s
not,
what are the main reasons for this “restriction”, and are there any
plans to
invent the possibility in 2.0, or so?)
I’ve tried to override the superclass and ancestors methods on a class,
which
is, of course, possible, but this does not change the behaviour of the
objects
of that class.
I really appreciate that you want to save me from myself…
But in Ruby it is possible to call private methods, you can change
the values of constants, you can read and write instance variables
form outside of an object. You can inherit from standard classes,
what’s more, you can reopen them and add/change/undef methods.
These (and many more) seem to be very bad ideas too. I’m just
curious why I can’t change a classes superclass while I can do
all the other magic.
What you do here is inserting a new Module into the ancestors chain.
(The superclass of class Dog doesn’t change, it’s still Animal.) If you
could do the same with a Stuff class (not module), that would be the
right answer to my stupid question.
The ancestry chain cannot be modified except for adding stuff in a
restricted way. That is the way the language works, you cannot insert in
arbitrary places, replace, reorder, or remove. There is no API.
Language designers would know the rationale, I don’t.