From: "T. Onoma" Date: 2003-12-17T04:16:32+09:00 Subject: Re: Copying methods from one class to another On Tuesday 16 December 2003 07:32 pm, David A. Black wrote: > > class C > > # don't even need the def here > > 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) > > } > > self.remove_method(m) > > end > > end > > > > class C # this is what I forgot: to add a method > > def x > > print "<"; super > > end > > end > > > > c = C.new > > c.x > > That code gives me: > > redef.rb:5: warning: already initialized constant C > redef.rb:13:in `method_added': private method `remove_method' called > for C:Class (NoMethodError) > from redef.rb:18 Agghhh...I sware I ran it before I posted it, hmm.... oh, duh! I added the self on remove_method. (Note to self: DO NOT touch up code for email posts after putting them in email message!) Here, I promise this one "works" and is more concise thanks to some other posts I was able to learn from. class C end class C < C; def self.method_added(m) meth = self.instance_method(m) self.superclass.__send__(:define_method, m, meth) self.__send__(:remove_method, m) end end class C def x print "<"; super end end c = C.new c.x -- T.