[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to copy a method from one class to another

Sam Kong

9/25/2006 4:07:00 PM

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.

Thanks.

Sam

16 Answers

Jeff Schwab

9/25/2006 4:24:00 PM

0

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.
>
> Thanks.
>
> Sam
>

Can you factor f out to a separate mixin?

module F
def f
p self.class
end
end

class C1
include F
end

class C2
include F
end

C1.new.f
C2.new.f

Logan Capaldo

9/25/2006 4:25:00 PM

0

On Tue, Sep 26, 2006 at 01:10:17AM +0900, 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.
>
You can inherit.

class C2 < C1
end

Or you can make C1 a module

module C1
def f
p self.class
end
end

class C2
include C1
end


Paul Lutus

9/25/2006 4:27:00 PM

0

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.
>
> Thanks.

Perhaps it would be better for you to describe the problem this idea would
address. If you need to have a shared method, maybe it would be better to
put it in a module and associate the module with any classes needing the
method. Or a parent-child relationship can be established between classes
to make a particular method available to more than one class.

--
Paul Lutus
http://www.ara...

Sam Kong

9/25/2006 5:27:00 PM

0


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

MonkeeSage

9/25/2006 5:38:00 PM

0

Sam Kong wrote:
> 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.

Unless I misunderstand, a mixin is fine:

module C1
def f
p self.class
end
end

class C2; end

class C3 < C2
include C1
end

C3.new.f # => "C3"

Regards,
Jordan

Jeff Schwab

9/25/2006 6:11:00 PM

0

Sam Kong wrote:
> 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 theb
> source.
>
> Can we still copy(or reuse) the method?

If the method only exists in a library class, how do you know what it
would do to your class? It sounds like the best way to get the effect
you want might be the same thing you would do in C++ or Java, viz. to
let C2 have a member of type C1, and forward method calls to the member
object according to the "Law" of Demeter.

Verno Miller

9/25/2006 6:13:00 PM

0

>Posted by Sam Kong
>on 25.09.2006 18:11
>
> Hi Rubyists,
>
> Is there a way to copy an instance method from one class to another?


How about using something like "inheritance and super function"?

http://www.ruby-...t...

--
Posted via http://www.ruby-....

Sam Kong

9/25/2006 6:29:00 PM

0

Hi Jordan,

Jordan Callicoat wrote:
> Sam Kong wrote:
>> 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.
>
> Unless I misunderstand, a mixin is fine:
>
> module C1
> def f
> p self.class
> end
> end

Unfortunately, C1 is already a class.

Sam

--
Posted via http://www.ruby-....

Sam Kong

9/25/2006 6:35:00 PM

0

Hi Verno,

Verno Miller wrote:
>>Posted by Sam Kong
>>on 25.09.2006 18:11
>>
>> Hi Rubyists,
>>
>> Is there a way to copy an instance method from one class to another?
>
>
> How about using something like "inheritance and super function"?
>
> http://www.ruby-...t...

But C1 and C2 are in different class hierarchies.
C2 is not a kind of C1.
What I'm trying to do is to copy(or reuse) the implementation of another
class in a different hierarchy.

Sam

--
Posted via http://www.ruby-....

Sam Kong

9/25/2006 6:46:00 PM

0

Hi Jeffrey,

Jeffrey Schwab wrote:

> If the method only exists in a library class, how do you know what it
> would do to your class? It sounds like the best way to get the effect
> you want might be the same thing you would do in C++ or Java, viz. to
> let C2 have a member of type C1, and forward method calls to the member
> object according to the "Law" of Demeter.

Yes, I think you're entirely right on this.
If C2 doesn't know how C1#f is implemented, it's no used copying the
method's implementation or even copying the implementation won't work
in C2's context.
Now I know that my imaginary problem was totally non-sense.

Thank you for enlightening me.

Sam