# Call overwritten method of included module?

**URL:** <https://rubytalk.org/t/call-overwritten-method-of-included-module/42432>\
**Category:** ruby-talk\
**Created:** [22 November 2007 23:21 UTC](https://rubytalk.org/t/call-overwritten-method-of-included-module/42432 "2007-11-22T23:21:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joshua\_Muheim](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@Joshua\_Muheim](https://rubytalk.org/u/Joshua_Muheim)\
**Post date:** [22 November 2007 23:21 UTC](https://rubytalk.org/t/call-overwritten-method-of-included-module/42432/1 "2007-11-22T23:21:47Z")

</div>

Hi all

module MyModule  
&nbsp;&nbsp;def my\_method  
&nbsp;&nbsp;&nbsp;&nbsp;return "my module rocks!"  
&nbsp;&nbsp;end  
end

class MyClass  
&nbsp;&nbsp;include(MyModule)

&nbsp;&nbsp;def my\_method  
&nbsp;&nbsp;&nbsp;&nbsp;return "my class rocks!"  
&nbsp;&nbsp;end  
end

Is there a way to call MyModule::my\_method from within MyClass? "super"  
sadly doesn't work...

Thanks a lot  
Josh

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Morton\_Goldberg](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Morton\_Goldberg](https://rubytalk.org/u/Morton_Goldberg)\
**Post date:** [23 November 2007 01:33 UTC](https://rubytalk.org/t/call-overwritten-method-of-included-module/42432/2 "2007-11-23T01:33:14Z")

</div>

Perhaps this will do:

\<code\>  
module MyModule  
&nbsp;&nbsp;&nbsp;&nbsp;def my\_method  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"my module rocks!"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

class MyClass  
&nbsp;&nbsp;&nbsp;&nbsp;include MyModule  
&nbsp;&nbsp;&nbsp;&nbsp;alias module\_method my\_method  
&nbsp;&nbsp;&nbsp;&nbsp;def my\_method  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"#{module\_method}\nmy class rocks!"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

puts MyClass.new.my\_method  
\</code\>

\<result\>  
my module rocks!  
my class rocks!  
\</result\>

Regards, Morton

> **···**
>
> On Nov 22, 2007, at 6:21 PM, Joshua Muheim wrote:
> 
> > Hi all
> > 
> > module MyModule  
> > &nbsp;&nbsp;def my\_method  
> > &nbsp;&nbsp;&nbsp;&nbsp;return "my module rocks!"  
> > &nbsp;&nbsp;end  
> > end
> > 
> > class MyClass  
> > &nbsp;&nbsp;include(MyModule)
> > 
> > &nbsp;&nbsp;def my\_method  
> > &nbsp;&nbsp;&nbsp;&nbsp;return "my class rocks!"  
> > &nbsp;&nbsp;end  
> > end
> > 
> > Is there a way to call MyModule::my\_method from within MyClass? "super"  
> > sadly doesn't work...

---

<div class="post-metadata">

**Author:** ![Daniel\_Sheppard](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@Daniel\_Sheppard](https://rubytalk.org/u/Daniel_Sheppard)\
**Post date:** [23 November 2007 02:30 UTC](https://rubytalk.org/t/call-overwritten-method-of-included-module/42432/3 "2007-11-23T02:30:19Z")

</div>

> module MyModule  
> &nbsp;&nbsp;def my\_method  
> &nbsp;&nbsp;&nbsp;&nbsp;return "my module rocks!"  
> &nbsp;&nbsp;end  
> end
> 
> class MyClass  
> &nbsp;&nbsp;include(MyModule)
> 
> &nbsp;&nbsp;def my\_method  
> &nbsp;&nbsp;&nbsp;&nbsp;return "my class rocks!"  
> &nbsp;&nbsp;end  
> end
> 
> Is there a way to call MyModule::my\_method from within  
> MyClass? "super"  
> sadly doesn't work...

You'd need to alias the old method:

class MyClass  
&nbsp;&nbsp;include(MyModule)  
&nbsp;&nbsp;alias :old\_my\_method :my\_method  
&nbsp;&nbsp;def my\_method  
&nbsp;&nbsp;&nbsp;&nbsp;old\_my\_method  
&nbsp;&nbsp;end  
end

(If you're using rails/activesupport, you'd probably use  
alias\_method\_chain to simplify things)

Dan.

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [23 November 2007 12:31 UTC](https://rubytalk.org/t/call-overwritten-method-of-included-module/42432/4 "2007-11-23T12:31:01Z")

</div>

> Hi all
> 
> module MyModule  
> &nbsp;&nbsp;def my\_method  
> &nbsp;&nbsp;&nbsp;&nbsp;return "my module rocks!"  
> &nbsp;&nbsp;end  
> end
> 
> class MyClass  
> &nbsp;&nbsp;include(MyModule)
> 
> &nbsp;&nbsp;def my\_method  
> &nbsp;&nbsp;&nbsp;&nbsp;return "my class rocks!"  
> &nbsp;&nbsp;end  
> end
> 
> Is there a way to call MyModule::my\_method from within MyClass? "super"  
> sadly doesn't work...

super shall work and indeed does  
module MyModule  
&nbsp;&nbsp;&nbsp;def my\_method  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return "my module rocks!"  
&nbsp;&nbsp;&nbsp;end  
end

class MyClass  
&nbsp;&nbsp;&nbsp;include(MyModule)

&nbsp;&nbsp;&nbsp;def my\_method

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return [super, "my class rocks!"].join("\n")  
&nbsp;&nbsp;&nbsp;end  
end

puts MyClass.new.my\_method

HTH  
Robert

> **···**
>
> On Nov 23, 2007 12:21 AM, Joshua Muheim \<forum@josh.ch\> wrote:
> 
> > Thanks a lot  
> > Josh  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> what do I think about Ruby?  
> [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

---

<div class="post-metadata">

**Author:** ![Joshua\_Muheim](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@Joshua\_Muheim](https://rubytalk.org/u/Joshua_Muheim)\
**Post date:** [23 November 2007 12:22 UTC](https://rubytalk.org/t/call-overwritten-method-of-included-module/42432/5 "2007-11-23T12:22:56Z")

</div>

> (If you're using rails/activesupport, you'd probably use  
> alias\_method\_chain to simplify things)
> 
> Dan.

Thanks! Especially for the alias\_method\_chain hint! 🙂

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
