From: "T. Onoma" Date: 2003-12-16T18:40:23+09:00 Subject: Re: Copying methods from one class to another On Tuesday 16 December 2003 05:23 am, Jamis Buck wrote: > T. Onoma wrote: > >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). 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. -- T.