I want to copy code from one class to another at runtime.
···
class A
def a
end
end
class B < Other
end
copy(A, B) #do magic
B.new.a
The easiest way would be inheritance of course, but
the receiving class has its own inheritance structure
and there is no is_a relation between these classes.
The most elegant way would be the use of the standard
include-mechanism of ruby, but this works only for modules.
(Is there a way to turn a class into a module?)
The third way I encountered, is the use of UnboundMethod’s.
I thought it might be possible to copy all methods from
one class to another class, but this does not work as expected.
Is there a possibilty to reeavaluate the code of A in the
context of B?
Anybody has an idea, how to achieve this cut&paste problem?
I want to copy code from one class to another at runtime.
class A
def a
end
end
class B < Other
end
copy(A, B) #do magic
…
The most elegant way would be the use of the standard
include-mechanism of ruby, but this works only for modules.
(Is there a way to turn a class into a module?)
How about just putting the code intoa module and including it on both A
and B?
Move the methods into a module M.
Include M in both A and B.
No duplication of code that way, and
no inheritance.
How is that?
There is probably an esoteric solution that
will literally do exactly what you wanted…
but I can’t think of a way to do it yet.
module R
class A
end
class B
def a
return A.new
end
end
end
module T
class A
copy_from(R::A) #magic copy
end
class B
copy_from(R::B) #magic copy
end
end
What i want to achieve is described best with “copy inheritance”
no real inheritance, more like copy the source code from one
class to another (as easy as in every editor).
The call T::B.new.a shall return a T::A not an R::A.
With the module approach it looks like:
module R
class A
end
module B_
def a
return A.new
new
end
class B
include B_
end
end
module T
class A
end
class B
include R::B_
end
end
Because the module B_ gets its binding in context of R, A.new
means R::A.new. Thus the include in T::B ought to do something
like rebind after include.
module T
def meth1
end
def meth2
end
def meth3
end
end
class A
include T
more stuff?
end
class B < Other
include T
more stuff?
end
What’s wrong with this?
Hal Fulton
···
----- Original Message -----
From: “Matthias Veit” matthias_veit@yahoo.de
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, July 15, 2002 5:29 PM
Subject: Re: cut & paste for code
What i want to achieve is described best with “copy inheritance”
no real inheritance, more like copy the source code from one
class to another (as easy as in every editor).
The call T::B.new.a shall return a T::A not an R::A.
What i want to achieve is described best with “copy inheritance”
no real inheritance, more like copy the source code from one
class to another (as easy as in every editor).
The call T::B.new.a shall return a T::A not an R::A.
Does this work for you?
module MA
end
module MB
def a
return type::A.new
end
end
module R
class A
include MA
end
class B
A = R::A
include MB
end
end
module T
class A
include MA
end
class B
A = T::A
include MB
end
end
I just mentioned, that the behaviour of the including code
depends on the surrounding context. If there is no context,
(the module T above is toplevel), no problem arises.
The problem, I see, are nested classes - there is a difference
between including a module (bound to a specific namespace) or
copyinheritance, where the binding is not done.