Cut & paste for code

Hi,

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?

Best regards,
Matthias


Do You Yahoo!?
Sign up for SBC Yahoo! Dial - First Month Free
http://sbc.yahoo.comm

Matthias Veit matthias_veit@yahoo.de writes:

Hi,

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?

Dave

I’m thinking about this.

The most elegant solution I can
come up with right now is:

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.

I started thinking along the lines of
B = A.dup, but of course that simply replaces
the old B (wiping out inheritance, etc.).

Probably Guy Decoux could do it in a single
line of code consisting only of punctuation
marks. :slight_smile:

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 1:55 PM
Subject: cut & paste for code

I want to copy code from one class to another at runtime.

I really like this solution, but I think my problem
is even more esoteric. Following situation:

···

HEF == “Hal E. Fulton” hal9000@hypermetrics.com wrote:

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.

Do you have any idea?

Best regards,
Matthias


Do You Yahoo!?
Yahoo! Autos - Get free new car price quotes
http://autos.yahoo.comm

[snip]

What I meant was more like this:

class Other

whatever

end

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.

With the module approach it looks like:

Matthias Veit wrote:

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

p R::B.new.a.type # prints: R::A

p T::B.new.a.type # prints: T::A

Absolutely nothing. It’s nice.

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.

Best regards,
Matthias

···

HEF == “Hal E. Fulton” hal9000@hypermetrics.com wrote:

[snip]

What I meant was more like this:

class Other

whatever

end

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?


Do You Yahoo!?
Yahoo! Autos - Get free new car price quotes
http://autos.yahoo.comm

Wow !! - solving the dependency explicitely is a good idea.
I have to figure out, if this is the way to do.

Thank you for now,
Matthias

···

JV == Joel VanderWerf vjoel@PATH.Berkeley.EDU wrote:

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

p R::B.new.a.type # prints: R::A

p T::B.new.a.type # prints: T::A


Do You Yahoo!?
Sign up for SBC Yahoo! Dial - First Month Free
http://sbc.yahoo.comm