# Copy methods from one module to another, with a twist

**URL:** <https://www.ruby-forum.com/t/copy-methods-from-one-module-to-another-with-a-twist/103903>\
**Category:** Ruby\
**Created:** [May 11, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/copy-methods-from-one-module-to-another-with-a-twist/103903 "2007-05-11T14:46:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [May 11, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/copy-methods-from-one-module-to-another-with-a-twist/103903/1 "2007-05-11T14:46:19Z")

</div>

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@:4(irb)  
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

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [May 12, 2007, 7:38pm UTC](https://www.ruby-forum.com/t/copy-methods-from-one-module-to-another-with-a-twist/103903/2 "2007-05-12T19:38:10Z")

</div>

2007/5/11, David C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> irb(main):003:0\> mod2 = Mod.new  
> =\> #Thing:0x67034  
> irb(main):012:0\> Other.send :include, mod2  
> mod2.send :include, :mod1  
> mod2.send :extend, :mod1
> 
> What’s the right way to do this?
> 
> Thanks,  
> David

Before including module mod2 in class Other, extend module mod2 with  
the “included” callback:

irb(main):011:0\> Mod1 = mod1 #Must start with capital apparently;  
otherwise, fails later on  
=\> Mod1  
irb(main):012:0\> module TempIncludeMod1  
irb(main):013:1\> def included( klass)  
irb(main):014:2\> klass.send :include, Mod1  
irb(main):015:2\> end  
irb(main):016:1\> end  
=\> nil  
irb(main):017:0\> mod2.extend( TempIncludeMod1)  
=\> #Mod:0x2b0ef9273520  
irb(main):018:0\> class Other; end  
=\> nil  
irb(main):019:0\> Other.send :include, mod2  
=\> Other  
irb(main):020:0\> o = Other.new  
=\> #Other:0x2b0ef91f26f0  
irb(main):021:0\> o.mod1\_method  
=\> “I’m defined in mod1”

Regards,  
Raf

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [May 12, 2007, 7:58pm UTC](https://www.ruby-forum.com/t/copy-methods-from-one-module-to-another-with-a-twist/103903/3 "2007-05-12T19:58:12Z")

</div>

2007/5/11, David C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> irb(main):003:0\> mod2 = Mod.new  
> =\> #Thing:0x67034  
> irb(main):012:0\> Other.send :include, mod2  
> mod2.send :include, :mod1  
> mod2.send :extend, :mod1
> 
> What’s the right way to do this?
> 
> Thanks,  
> David

Erm, actually forget my previous reply, it’s all much simpler: simply  
include module Mod1 in module Mod2 before including module Mod2 in  
class Other:

module Mod1  
def mod1\_method  
“I’m defined in Mod1”  
end  
end

module Mod2  
include Mod1  
end

class Thing  
include Mod1  
end

class Other  
include Mod2  
end

p Thing.new.mod1\_method # =\> “I’m defined in Mod1”  
p Other.new.mod1\_method # =\> “I’m defined in Mod1”

Remark that there is no need to subclass Module in fact, because  
you’re not at all extending class Module.

Regards,  
Raf
