Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to copy a method from one class to another

Reply
Thread Tools

How to copy a method from one class to another

 
 
Sam Kong
Guest
Posts: n/a
 
      09-25-2006
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

 
Reply With Quote
 
 
 
 
Logan Capaldo
Guest
Posts: n/a
 
      09-25-2006
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


 
Reply With Quote
 
 
 
 
Sam Kong
Guest
Posts: n/a
 
      09-25-2006

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

 
Reply With Quote
 
MonkeeSage
Guest
Posts: n/a
 
      09-25-2006
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

 
Reply With Quote
 
Verno Miller
Guest
Posts: n/a
 
      09-25-2006
>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-forum.com/topic/81049

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

 
Reply With Quote
 
Sam Kong
Guest
Posts: n/a
 
      09-25-2006
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-forum.com/.

 
Reply With Quote
 
Sam Kong
Guest
Posts: n/a
 
      09-25-2006
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-forum.com/topic/81049


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-forum.com/.

 
Reply With Quote
 
Sam Kong
Guest
Posts: n/a
 
      09-25-2006
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

 
Reply With Quote
 
Florian Frank
Guest
Posts: n/a
 
      09-25-2006
MonkeeSage 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:
>

This wouldn't work in the situation Sam describes:

class Rubyforge::C1 # for example in some rubyforge library
def interesting_method
if @foo > 3
@bar += 2
else
@baz /= 2
end
end
end

Sam wonders how to call interesting_method from his class C2, that
cannot inherit
from C1, because it already inherits from SomeOtherClass:

class C2 < SomeOtherClass
def my_method
interesting_method # ???
end
end

This isn't possible in Ruby, because of single inheritance and the fact
that Sam doesn't control the rubyforge library and thus cannot create a
module to include in C2. The only way to get interesting_method's
functionality into C2 is to copy & paste it.

If there was a way in Ruby to do that like:

class C2 < SomeOtherClass
include Rubyforge::C1.to_module(:interesting_method)

def my_method
interesting_method
end
end

This would introduce a tight coupling between C2 and C1 on the same
level like inheritance would do. In some way it would be equivalent to
real multiple inheritance. Now the author of C1 can break your code by
refactoring e. g.:

class Rubyforge::C1 # for example in some rubyforge library
def interesting_method
if @foo > 3
add_two
else
@baz /= 2
end
end

private

def add_two
@bar += 2
end
end

I was in the same situation and ended up copying & pasting. I wished at
the time, that it would have been possible to just import those methods,
even if it meant that I might ending up shooting myself into the foot.

There is an additional problem, that occurs when someone attempts to
import methods from a Ruby class implemented in C, that uses a macro
like RSTRING(foo)->len. In these cases it would be possible to trigger a
Ruby segmentation fault by calling those methods. Of course it would
perhaps be possible to mark C implemented methods and let Ruby refuse to
export them. This would at least cause an ordinary exception during
class loading time.

--
Florian Frank


 
Reply With Quote
 
Florian Frank
Guest
Posts: n/a
 
      09-25-2006
Sam Kong 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.
>

If this is possible, it is a better solution of course. I think in
general it's better to use delegation than to use inheritance for code
sharing purposes, using modules is maybe on par with delegation.
>
> 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.
>

Ok, let me switch off the light again. You would read the source code
before you do it and decide if you can prepare instance variables and
methods in the required way. I would call that "internal duck typing".
Of course this would make it necessary to study the source code of C1 to
find out what exactly it does. But this isn't so different from what you
have to do if you want to inherit from the class (especially in Ruby).

--
Florian Frank


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
injecting an instance method into another class' class method Chuck Remes Ruby 3 08-12-2010 07:15 PM
Adding method from one class to another class or to instance ofanother class marekw2143 Python 3 07-25-2009 08:33 PM
why a class can't access protected method from another class in thesame package,the method is interited from the ohtner class from differntpackage? junzhang1983@gmail.com Java 3 01-28-2008 02:09 AM
Can a method in one class change an object in another class? Stewart Midwinter Python 5 03-07-2005 03:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57