copy methods from one module to another, with a twist

D

David Chelimsky

Hi all. I'm trying to figure out how to copy methods from one module
to another (actually subclasses of Module because I don't want to
actually extend Module in this context) and I could use some help.

The following works fine.

irb(main):001:0> class Mod < Module; end
=> nil
irb(main):002:0> mod1 = Mod.new
=> #<Mod:0x6108>
irb(main):003:0> mod2 = Mod.new
=> #<Mod:0x89198>
irb(main):004:0> mod1.send :define_method, :mod1_method do
irb(main):005:1* "I'm defined in mod1"
irb(main):006:1> end
=> #<Proc:0x0007d780@(irb):4>
irb(main):007:0> class Thing; end
=> nil
irb(main):008:0> Thing.send :include, mod1
=> Thing
irb(main):009:0> t = Thing.new
=> #<Thing:0x67034>
irb(main):010:0> t.mod1_method
=> "I'm defined in mod1"

What I have to do, however, is more complex. I need to copy
#mod1_method to mod2, and I only have access to the two modules when I
want to do this (not the classes/objects that include them). So,
adding to the above ...

irb(main):011:0> class Other; end
=> nil
irb(main):012:0> Other.send :include, mod2
=> Other
irb(main):013:0> o = Other.new
=> #<Other:0x57ad0>

I want to get from here to the following:

o.mod1_method

I've tried these approaches:

mod2.send :include, :mod1
mod2.send :extend, :mod1

What's the right way to do this?

Thanks,
David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top