Copying methods from one class to another

J

Jamis Buck

T. Onoma said:
Is there any way to copy a method from one class to another?

I'd like to know the answer to this one, too, though I suspect there
isn't going to be a way. If such a feature existed, then in order to be
safe in copying a method from one class to another, there should also be
a feature that allowed you to query which methods a method depends on,
and that can be a tough question to answer (perhaps even impossible,
though I couldn't say that with confidence).
 
T

T. Onoma

I'd like to know the answer to this one, too, though I suspect there
isn't going to be a way. If such a feature existed, then in order to be
safe in copying a method from one class to another, there should also be
a feature that allowed you to query which methods a method depends on,
and that can be a tough question to answer (perhaps even impossible,
though I couldn't say that with confidence).

At first it seemed like it would be easy, since Ruby is so dynamic --if I add
a method to a class, every object previously defined as that class instantly
has it. But then I discoverd that the super keyword appear "prebound" to its
superclass. Take a look at:

class C
def x
super
end
end

class C < C;
@__aspect__ = true
def self.method_added(m)
meth = self.instance_method(m)
self.superclass.class_eval %Q{
remove_method:)#{m}) if method_defined?:)m)
define_method:)#{m}, meth)
}
remove_method(m)
end
end
c = C.new
c.x

I have satisfied the condition of the binding class being the same class type,
but nonetheless this code causes an infinite recursion on the super call.

So now I'm guessing the problem is one of closure.
 

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
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top