# Module to overwrite method defined via define\_method

**URL:** <https://rubytalk.org/t/module-to-overwrite-method-defined-via-define-method/55351>\
**Category:** ruby-talk\
**Created:** [13 September 2009 16:05 UTC](https://rubytalk.org/t/module-to-overwrite-method-defined-via-define-method/55351 "2009-09-13T16:05:07Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gaspard\_Bucher1](https://avatars.discourse-cdn.com/v4/letter/g/e9bcb4/32.png) [@Gaspard\_Bucher1](https://rubytalk.org/u/Gaspard_Bucher1)\
**Post date:** [13 September 2009 16:05 UTC](https://rubytalk.org/t/module-to-overwrite-method-defined-via-define-method/55351/1 "2009-09-13T16:05:07Z")

</div>

Hi List !

I found a tricky thing: if you use define\_method and then include a  
module that contains a method with the same name, the method in the  
module is just ignored.

Why is that so ?

How can I overwrite a method defined through 'define\_method' ?

Example ruby code: [http://bit.ly/2NjP5w](http://bit.ly/2NjP5w)

Gaspard

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

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [13 September 2009 16:11 UTC](https://rubytalk.org/t/module-to-overwrite-method-defined-via-define-method/55351/2 "2009-09-13T16:11:56Z")

</div>

Hi --

> Hi List !
> 
> I found a tricky thing: if you use define\_method and then include a  
> module that contains a method with the same name, the method in the  
> module is just ignored.
> 
> Why is that so ?
> 
> How can I overwrite a method defined through 'define\_method' ?
> 
> Example ruby code: [gist:186219 · GitHub](http://bit.ly/2NjP5w)

It's a matter of the order of method lookup. In general, an object  
looks for a method first in its class, and then in modules mixed into  
that class.

&nbsp;&nbsp;&nbsp;module M  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def x; puts "x in M"; end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;class A  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def x; puts "x in A"; end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include M  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;A.new.x # x in A

It's the same if the method is defined with #define\_method.

Yehuda Katz has recently proposed that there be a way to insert a  
module in the lookup order before the class. I'm not sure where that  
proposal stands at the moment.

David

> **···**
>
> On Mon, 14 Sep 2009, Gaspard Bucher wrote:
> 
> --  
> David A. Black, Director  
> Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))  
> Ruby/Rails training, consulting, mentoring, code review  
> Book: The Well-Grounded Rubyist ([http://www.manning.com/black2\](http://www.manning.com/black2%5C))

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [13 September 2009 18:25 UTC](https://rubytalk.org/t/module-to-overwrite-method-defined-via-define-method/55351/3 "2009-09-13T18:25:20Z")

</div>

Gaspard Bucher wrote:

> Hi List !
> 
> I found a tricky thing: if you use define\_method and then include a  
> module that contains a method with the same name, the method in the  
> module is just ignored.
> 
> Why is that so ?

By the way, you can get the lookup order using the ancestors method:

module Redef  
&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;puts "'foo' from module"  
&nbsp;&nbsp;end  
end

class A  
&nbsp;&nbsp;define\_method("foo") do  
&nbsp;&nbsp;&nbsp;&nbsp;puts "'foo' from define\_method"  
&nbsp;&nbsp;end

&nbsp;&nbsp;include Redef  
end

p A.ancestors

--output:--  
[A, Redef, Object, Kernel]

Although, ancestors() doesn't include the singleton classes:

module Redef  
&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;puts "'foo' from module"  
&nbsp;&nbsp;end  
end

class A  
&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;puts "foo from class A"  
&nbsp;&nbsp;end

&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include Redef  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

end

a = A.new  
a.foo  
p A.ancestors

--output:--  
[A, Object, Kernel]

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

---

<div class="post-metadata">

**Author:** ![Gaspard\_Bucher1](https://avatars.discourse-cdn.com/v4/letter/g/e9bcb4/32.png) [@Gaspard\_Bucher1](https://rubytalk.org/u/Gaspard_Bucher1)\
**Post date:** [13 September 2009 16:18 UTC](https://rubytalk.org/t/module-to-overwrite-method-defined-via-define-method/55351/4 "2009-09-13T16:18:52Z")

</div>

David A. Black wrote:

> Hi --
> 
> > Example ruby code: [gist:186219 · GitHub](http://bit.ly/2NjP5w)
> 
> It's a matter of the order of method lookup. In general, an object  
> looks for a method first in its class, and then in modules mixed into  
> that class.

Damned ! This means that all the dynamic methods created by rails with  
"has\_one" and such cannot be overwritten by including a module except if  
you write:

module A  
&nbsp;&nbsp;def self.included(base)  
&nbsp;&nbsp;&nbsp;&nbsp;base.send(:define\_method, 'foo') do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "'foo' from A"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

Any other solution ?

Gaspard

> **···**
>
> > On Mon, 14 Sep 2009, Gaspard Bucher wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Gaspard\_Bucher1](https://avatars.discourse-cdn.com/v4/letter/g/e9bcb4/32.png) [@Gaspard\_Bucher1](https://rubytalk.org/u/Gaspard_Bucher1)\
**Post date:** [13 September 2009 18:32 UTC](https://rubytalk.org/t/module-to-overwrite-method-defined-via-define-method/55351/5 "2009-09-13T18:32:57Z")

</div>

7stud -- wrote:

> class A  
> &nbsp;&nbsp;def foo  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "foo from class A"  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;class \<\< self  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include Redef  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> end

Thanks for the note 7stud. I think you can also write the above example  
as:

class A  
&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;puts "foo from A"  
&nbsp;&nbsp;end

&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;extend Redef  
&nbsp;&nbsp;end  
end

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

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [13 September 2009 17:01 UTC](https://rubytalk.org/t/module-to-overwrite-method-defined-via-define-method/55351/6 "2009-09-13T17:01:22Z")

</div>

Hi --

> David A. Black wrote:
> 
> > Hi --
> > 
> > > Example ruby code: [gist:186219 · GitHub](http://bit.ly/2NjP5w)
> > 
> > It's a matter of the order of method lookup. In general, an object  
> > looks for a method first in its class, and then in modules mixed into  
> > that class.
> 
> Damned ! This means that all the dynamic methods created by rails with  
> "has\_one" and such cannot be overwritten by including a module except if  
> you write:
> 
> module A  
> def self.included(base)  
> &nbsp;&nbsp;&nbsp;base.send(:define\_method, 'foo') do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "'foo' from A"  
> &nbsp;&nbsp;&nbsp;end  
> end  
> end
> 
> Any other solution ?

I would consider rewriting that as:

&nbsp;&nbsp;&nbsp;module A  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.included(base)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;base.class\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "'foo' from A"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

so as to normalize it back to the "def" form.

Keep in mind, too, that this is a bit fragile because the order  
matters. If you include the module first and then do has\_many, (or  
attr\_accessor, or any other instance-method generator), the has\_many  
will "win".

David

> **···**
>
> On Mon, 14 Sep 2009, Gaspard Bucher wrote:
> 
> > > On Mon, 14 Sep 2009, Gaspard Bucher wrote:
> 
> --  
> David A. Black, Director  
> Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))  
> Ruby/Rails training, consulting, mentoring, code review  
> Book: The Well-Grounded Rubyist ([http://www.manning.com/black2\](http://www.manning.com/black2%5C))

---

<div class="post-metadata">

**Author:** ![Caleb\_Clausen1](https://avatars.discourse-cdn.com/v4/letter/c/8dc957/32.png) [@Caleb\_Clausen1](https://rubytalk.org/u/Caleb_Clausen1)\
**Post date:** [13 September 2009 17:42 UTC](https://rubytalk.org/t/module-to-overwrite-method-defined-via-define-method/55351/7 "2009-09-13T17:42:33Z")

</div>

If you use extend instead of include, the methods in the module should  
be seen first. Extend operates on instances, not classes, tho, so  
you'll have to call extend in your initialize.

Alternately, if you make a subclass and then use include in the  
subclass, I believe that will also utilize the module version. Not  
sure how that might interoperate with rails.

> **···**
>
> On 9/13/09, Gaspard Bucher \<gaspard@teti.ch\> wrote:
> 
> > Damned ! This means that all the dynamic methods created by rails with  
> > "has\_one" and such cannot be overwritten by including a module except if  
> > you write:
> > 
> > module A  
> > &nbsp;&nbsp;def self.included(base)  
> > &nbsp;&nbsp;&nbsp;&nbsp;base.send(:define\_method, 'foo') do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "'foo' from A"  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > end
> > 
> > Any other solution ?

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [13 September 2009 17:48 UTC](https://rubytalk.org/t/module-to-overwrite-method-defined-via-define-method/55351/8 "2009-09-13T17:48:05Z")

</div>

Gaspard Bucher wrote:

> Any other solution ?

module Redef  
&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;puts "'foo' from module"  
&nbsp;&nbsp;end  
end

class A  
&nbsp;&nbsp;def sayhi  
&nbsp;&nbsp;&nbsp;&nbsp;puts "hi"  
&nbsp;&nbsp;end

&nbsp;&nbsp;define\_method("foo") do  
&nbsp;&nbsp;&nbsp;&nbsp;puts "'foo' from define\_method"  
&nbsp;&nbsp;end

&nbsp;&nbsp;include Redef  
end

class Wrapper  
&nbsp;&nbsp;include Redef

&nbsp;&nbsp;def initialize(a\_obj)  
&nbsp;&nbsp;&nbsp;&nbsp;@a = a\_obj  
&nbsp;&nbsp;end

&nbsp;&nbsp;def method\_missing(meth\_name, \*args)  
&nbsp;&nbsp;&nbsp;&nbsp;@a.send(meth\_name, \*args)  
&nbsp;&nbsp;end

end

a = A.new  
w = Wrapper.new(a)

w.sayhi #hi  
w.foo #'foo' from module  
w.non\_existent

r1test.rb:26:in `send': undefined method `non\_existent' for #\<A:0x24928\>  
(NoMethodError)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from r1test.rb:26:in `method\_missing'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from r1test.rb:35

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

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [13 September 2009 18:35 UTC](https://rubytalk.org/t/module-to-overwrite-method-defined-via-define-method/55351/9 "2009-09-13T18:35:33Z")

</div>

Gaspard Bucher wrote:

> Thanks for the note 7stud. I think you can also write the above example  
> as:
> 
> class A  
> &nbsp;&nbsp;def foo  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "foo from A"  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;extend Redef  
> &nbsp;&nbsp;end  
> end

Yes. I thought it might be clearer the other way.

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

---

<div class="post-metadata">

**Author:** ![David\_Masover](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@David\_Masover](https://rubytalk.org/u/David_Masover)\
**Post date:** [15 September 2009 00:50 UTC](https://rubytalk.org/t/module-to-overwrite-method-defined-via-define-method/55351/10 "2009-09-15T00:50:15Z")

</div>

As others said, drop the 'define\_method'. I'd take it a step further:

module A  
&nbsp;&nbsp;module ClassMethods  
&nbsp;&nbsp;&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "'foo' from A"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def self.included(base)  
&nbsp;&nbsp;&nbsp;&nbsp;base.send :extend, ClassMethods  
&nbsp;&nbsp;end  
end

This is a common idiom. It's not so much load order as the fact that class  
methods don't automatically get included by "include" -- but they are also  
instance methods on the class, if that makes sense.

The advantage of doing it this way is that you can put a lot more stuff in  
ClassMethods, or even mix in other modules (via "include") inside  
ClassMethods. It's also nice and self-documenting, and it's used all over the  
place -- I believe inside Rails, at least.

I would even go so far as to call this a best practice. Thoughts?

> **···**
>
> On Sunday 13 September 2009 11:18:52 am Gaspard Bucher wrote:
> 
> > Damned ! This means that all the dynamic methods created by rails with  
> > "has\_one" and such cannot be overwritten by including a module except if  
> > you write:
> > 
> > module A  
> > &nbsp;&nbsp;def self.included(base)  
> > &nbsp;&nbsp;&nbsp;&nbsp;base.send(:define\_method, 'foo') do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "'foo' from A"  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > end
> > 
> > Any other solution ?

---

<div class="post-metadata">

**Author:** ![Gaspard\_Bucher1](https://avatars.discourse-cdn.com/v4/letter/g/e9bcb4/32.png) [@Gaspard\_Bucher1](https://rubytalk.org/u/Gaspard_Bucher1)\
**Post date:** [13 September 2009 18:00 UTC](https://rubytalk.org/t/module-to-overwrite-method-defined-via-define-method/55351/11 "2009-09-13T18:00:29Z")

</div>

David,

Thanks for the highlights and the advice , I will consider keeping the  
'def xxx' syntax.

Gaspard

David A. Black wrote:

> **···**
>
> > I would consider rewriting that as:
> > 
> > &nbsp;&nbsp;&nbsp;module A  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.included(base)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;base.class\_eval do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def foo  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "'foo' from A"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > so as to normalize it back to the "def" form.
> > 
> > Keep in mind, too, that this is a bit fragile because the order  
> > matters. If you include the module first and then do has\_many, (or  
> > attr\_accessor, or any other instance-method generator), the has\_many  
> > will "win".
> > 
> > David
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [13 September 2009 18:34 UTC](https://rubytalk.org/t/module-to-overwrite-method-defined-via-define-method/55351/12 "2009-09-13T18:34:09Z")

</div>

Can anyone explain this one:

module Redef  
&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;puts "'foo' from module"  
&nbsp;&nbsp;end  
end

class A  
&nbsp;&nbsp;define\_method("foo") do  
&nbsp;&nbsp;&nbsp;&nbsp;puts "'foo' from define\_method"  
&nbsp;&nbsp;end

&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include Redef  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;#include Redef

end

p A.ancestors  
a = A.new  
a.foo

--output:--  
[A, Object, Kernel]  
'foo' from module

As expected, the version of foo in a's singleton class is found before  
the version of foo in class A in the method lookup before.

But what about here:

module Redef  
&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;puts "'foo' from module"  
&nbsp;&nbsp;end  
end

class A  
&nbsp;&nbsp;define\_method("foo") do  
&nbsp;&nbsp;&nbsp;&nbsp;puts "'foo' from define\_method"  
&nbsp;&nbsp;end

&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include Redef  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;include Redef #\<---\*\*\*\*\*CHANGE HERE

end

p A.ancestors  
a = A.new  
a.foo

I would expect the order of the look up for the foo method to be:

a's singleton class ==\> 'foo from module'  
a's class(=A) ==\> 'foo from define method'  
a's mixins(=Redef) ==\> 'foo from module'

But the output is:

[A, Redef, Object, Kernel]  
'foo' from define\_method

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

---

<div class="post-metadata">

**Author:** ![Gaspard\_Bucher1](https://avatars.discourse-cdn.com/v4/letter/g/e9bcb4/32.png) [@Gaspard\_Bucher1](https://rubytalk.org/u/Gaspard_Bucher1)\
**Post date:** [15 September 2009 08:30 UTC](https://rubytalk.org/t/module-to-overwrite-method-defined-via-define-method/55351/13 "2009-09-15T08:30:23Z")

</div>

I think your example does not correspond to our problem: with extend you  
are adding class methods to the included class and our problem was  
overwriting \*instance\* methods. In fact to avoid the "define\_method"  
inside the class priority thing this could be a better option by using  
an anonymous Module:

class Base  
&nbsp;&nbsp;def self.has\_one(thing)  
&nbsp;&nbsp;&nbsp;&nbsp;m = Module.new do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(thing) do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Has one #{thing}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;include m  
&nbsp;&nbsp;end  
end

module Redef  
&nbsp;&nbsp;def dog  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Super says:"  
&nbsp;&nbsp;&nbsp;&nbsp;super  
&nbsp;&nbsp;&nbsp;&nbsp;puts "But I say: it has nothing because I decide so."  
&nbsp;&nbsp;end  
end

class Dummy \< Base  
&nbsp;&nbsp;has\_one :dog  
&nbsp;&nbsp;include Redef  
end

Dummy.new.dog

David Masover wrote:

> **···**
>
> > On Sunday 13 September 2009 11:18:52 am Gaspard Bucher wrote:
> > 
> > > end
> > > 
> > > Any other solution ?
> > 
> > As others said, drop the 'define\_method'. I'd take it a step further:
> > 
> > module A  
> > &nbsp;&nbsp;module ClassMethods  
> > &nbsp;&nbsp;&nbsp;&nbsp;def foo  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "'foo' from A"  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;def self.included(base)  
> > &nbsp;&nbsp;&nbsp;&nbsp;base.send :extend, ClassMethods  
> > &nbsp;&nbsp;end  
> > end
> > 
> > This is a common idiom. It's not so much load order as the fact that  
> > class  
> > methods don't automatically get included by "include" -- but they are  
> > also  
> > instance methods on the class, if that makes sense.
> > 
> > The advantage of doing it this way is that you can put a lot more stuff  
> > in  
> > ClassMethods, or even mix in other modules (via "include") inside  
> > ClassMethods. It's also nice and self-documenting, and it's used all  
> > over the  
> > place -- I believe inside Rails, at least.
> > 
> > I would even go so far as to call this a best practice. Thoughts?
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [15 September 2009 10:24 UTC](https://rubytalk.org/t/module-to-overwrite-method-defined-via-define-method/55351/14 "2009-09-15T10:24:13Z")

</div>

Hi --

> > Damned ! This means that all the dynamic methods created by rails with  
> > "has\_one" and such cannot be overwritten by including a module except if  
> > you write:
> > 
> > module A  
> > &nbsp;&nbsp;def self.included(base)  
> > &nbsp;&nbsp;&nbsp;&nbsp;base.send(:define\_method, 'foo') do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "'foo' from A"  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > end
> > 
> > Any other solution ?
> 
> As others said, drop the 'define\_method'. I'd take it a step further:
> 
> module A  
> module ClassMethods  
> &nbsp;&nbsp;&nbsp;def foo  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "'foo' from A"  
> &nbsp;&nbsp;&nbsp;end  
> end  
> def self.included(base)  
> &nbsp;&nbsp;&nbsp;base.send :extend, ClassMethods

That's the long way round 🙂

&nbsp;&nbsp;&nbsp;base.extend(ClassMethods)

> I would even go so far as to call this a best practice. Thoughts?

The name "ClassMethods" is potentially a bit confusing, since they're  
not exactly class methods... but I think it's an effective way to do  
what it does. I don't think it's inherently a better practice than,  
say, extending a class explicitly in the class -- meaning, I wouldn't  
go out of my way to set it up this way if it didn't fall into place  
fairly naturally in a give case.

David

> **···**
>
> On Tue, 15 Sep 2009, David Masover wrote:
> 
> > On Sunday 13 September 2009 11:18:52 am Gaspard Bucher wrote:
> 
> --  
> David A. Black, Director  
> Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))  
> Ruby/Rails training, consulting, mentoring, code review  
> Book: The Well-Grounded Rubyist ([http://www.manning.com/black2\](http://www.manning.com/black2%5C))

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [13 September 2009 18:44 UTC](https://rubytalk.org/t/module-to-overwrite-method-defined-via-define-method/55351/15 "2009-09-13T18:44:10Z")

</div>

Hi --

> But what about here:
> 
> module Redef  
> def foo  
> &nbsp;&nbsp;&nbsp;puts "'foo' from module"  
> end  
> end
> 
> class A  
> define\_method("foo") do  
> &nbsp;&nbsp;&nbsp;puts "'foo' from define\_method"  
> end
> 
> def initialize  
> &nbsp;&nbsp;&nbsp;class \<\< self  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include Redef  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> include Redef #\<---\*\*\*\*\*CHANGE HERE
> 
> end
> 
> p A.ancestors  
> a = A.new  
> a.foo
> 
> I would expect the order of the look up for the foo method to be:
> 
> a's singleton class ==\> 'foo from module'  
> a's class(=A) ==\> 'foo from define method'  
> a's mixins(=Redef) ==\> 'foo from module'
> 
> But the output is:
> 
> [A, Redef, Object, Kernel]  
> 'foo' from define\_method

I believe what's happening is this: when you include Redef in a's  
singleton class, Ruby sees that it's already in the ancestors (since  
it's been included in A) and doesn't add it.

In this example, I've got one module that's included in A, and one  
isn't, and I extend my instance with both. As you can see, the Dummy  
module appears before A in the ancestor list for a's singleton class,  
whereas Redef doesn't.

module Redef  
&nbsp;&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "'foo' from module"  
&nbsp;&nbsp;&nbsp;end  
end

module Dummy  
end

class A  
&nbsp;&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "'foo' from A"  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;extend(Redef)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;extend(Dummy)  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;include Redef

end

a = A.new  
a.foo  
p A.ancestors  
class \<\< a; p ancestors; end

Output:

'foo' from A  
[A, Redef, Object, Kernel]  
[Dummy, A, Redef, Object, Kernel]

David

> **···**
>
> On Mon, 14 Sep 2009, 7stud -- wrote:
> 
> --  
> David A. Black, Director  
> Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))  
> Ruby/Rails training, consulting, mentoring, code review  
> Book: The Well-Grounded Rubyist ([http://www.manning.com/black2\](http://www.manning.com/black2%5C))

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [13 September 2009 20:39 UTC](https://rubytalk.org/t/module-to-overwrite-method-defined-via-define-method/55351/16 "2009-09-13T20:39:39Z")

</div>

Yep.

Ruby will only include a module once in the implementation chain, it  
turns out it's because of the way the super keyword is implemented.

I talked to Matz about why this was at RubyConf in 2007 and wrote  
about the conversation

[http://talklikeaduck.denhaven2.com/2007/11/03/a-chat-with-matz-classs-variable-reversion-and-a-mystery-explained\](http://talklikeaduck.denhaven2.com/2007/11/03/a-chat-with-matz-classs-variable-reversion-and-a-mystery-explained%5C).

> **···**
>
> On Sun, Sep 13, 2009 at 2:44 PM, David A. Black \<dblack@rubypal.com\> wrote:
> 
> > Hi --
> > 
> > On Mon, 14 Sep 2009, 7stud -- wrote:
> > 
> > > But what about here:
> > > 
> > > module Redef  
> > > def foo  
> > > puts "'foo' from module"  
> > > end  
> > > end
> > > 
> > > class A  
> > > define\_method("foo") do  
> > > puts "'foo' from define\_method"  
> > > end
> > > 
> > > def initialize  
> > > class \<\< self  
> > > include Redef  
> > > end  
> > > end
> > > 
> > > include Redef #\<---\*\*\*\*\*CHANGE HERE
> > > 
> > > end
> > > 
> > > p A.ancestors  
> > > a = A.new  
> > > a.foo
> > > 
> > > I would expect the order of the look up for the foo method to be:
> > > 
> > > a's singleton class ==\> 'foo from module'  
> > > a's class(=A) ==\> 'foo from define method'  
> > > a's mixins(=Redef) ==\> 'foo from module'
> > > 
> > > But the output is:
> > > 
> > > [A, Redef, Object, Kernel]  
> > > 'foo' from define\_method
> > 
> > I believe what's happening is this: when you include Redef in a's  
> > singleton class, Ruby sees that it's already in the ancestors (since  
> > it's been included in A) and doesn't add it.
> 
> --  
> Rick DeNatale
> 
> Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
> WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [13 September 2009 23:16 UTC](https://rubytalk.org/t/module-to-overwrite-method-defined-via-define-method/55351/17 "2009-09-13T23:16:44Z")

</div>

Thanks.

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