From: Sam Kong Date: 2006-09-26T02:30:18+09:00 Subject: Re: How to copy a method from one class to another Sam Kong wrote: > Hi Rubyists, > > Is there a way to copy an instance method from one class to another? > > class C1 > def f > p self.class > end > end > > class C2 > end > > Now I want C2 to have f method so that I can call C2.new.f. OK. I need to describe what I want. Actually this problem is rather imaginary than practical. While solving some other problem, this question came into my mind. Normally, I would use inheritance or mix-in or maybe delegation. This question is "What if...?" Let's say that there's a class (C1) in a library and I'm building my own class hierarchy including C2 (class C2 < SomeOtherClass). In such a case, Inheritance or mix-in is not an option. However, I want to add a method of C1 into my C2. As someone jokingly mentioned, I can copy the source code. Let's assume that the method is implemented in C and we don't have the source. Can we still copy(or reuse) the method? Sam