G
Giles Bowkett
I want to write a module which, when included in another module,
destroys every method of a class in that module. I realize this isn't
really a very useful thing to do, but I want to do it to see if it can
be done. I want to actually include the module and have it replace all
existing methods of this class with just one method.
In Python you can change an object's class in the middle of your
program by reassigning its magical __class__ variable. That would be a
very simple way to implement this idea. Doesn't seem possible here,
though.
Here's an attempt which failed:
NoMethodError: undefined method `show' for #<Muppet:0x123129c>
from (irb):36
from :0
So far so good, but I want to get rid of *every* method. Doing what I
just did programmatically, iterating over all available methods,
that's the thing which still eludes me.
None of these attempts work:
NameError: undefined local variable or method `m' for #<Class:Muppet>
from (irb):43
from (irb):43:in `each'
from (irb):43
from :0NameError: undefined local variable or method `m' for Muppet:Class
from (irb):44
from (irb):44:in `each'
from (irb):44
from :0NoMethodError: undefined method `remove_method' for #<Object:0x349f4>
from (irb):45
from (irb):45:in `each'
from (irb):45
from :0
I have to admit, it's not necessarily a bad thing if this task proves
impossible, but I feel like it *should* be possible.
destroys every method of a class in that module. I realize this isn't
really a very useful thing to do, but I want to do it to see if it can
be done. I want to actually include the module and have it replace all
existing methods of this class with just one method.
In Python you can change an object's class in the middle of your
program by reassigning its magical __class__ variable. That would be a
very simple way to implement this idea. Doesn't seem possible here,
though.
Here's an attempt which failed:
NoMethodError: undefined method `show' for #<Muppet:0x123129c>
from (irb):36
from :0
So far so good, but I want to get rid of *every* method. Doing what I
just did programmatically, iterating over all available methods,
that's the thing which still eludes me.
None of these attempts work:
NameError: undefined local variable or method `m' for #<Class:Muppet>
from (irb):43
from (irb):43:in `each'
from (irb):43
from :0NameError: undefined local variable or method `m' for Muppet:Class
from (irb):44
from (irb):44:in `each'
from (irb):44
from :0NoMethodError: undefined method `remove_method' for #<Object:0x349f4>
from (irb):45
from (irb):45:in `each'
from (irb):45
from :0
I have to admit, it's not necessarily a bad thing if this task proves
impossible, but I feel like it *should* be possible.