# Overriding Class Methods With Modules

**URL:** <https://www.ruby-forum.com/t/overriding-class-methods-with-modules/66947>\
**Category:** Ruby\
**Created:** [August 30, 2006, 3:37pm UTC](https://www.ruby-forum.com/t/overriding-class-methods-with-modules/66947 "2006-08-30T15:37:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_WSLong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@John\_WSLong](https://www.ruby-forum.com/u/John_WSLong)\
**Post date:** [August 30, 2006, 3:37pm UTC](https://www.ruby-forum.com/t/overriding-class-methods-with-modules/66947/1 "2006-08-30T15:37:13Z")

</div>

How do I override a class method on an object with another module? For  
example:

class TestObject  
def self.my\_method  
“override me”  
end  
end

module TestExtension  
def my\_method  
“overridden by TestExtension”  
end  
def another\_method  
“another method”  
end  
end

TestObject.extend TestExtension

puts TestObject.my\_method #=\> “override me”  
puts TestObject.another\_method #=\> “another method”

Why can’t I get TestObject to use my\_method from TestExtension?

---

<div class="post-metadata">

**Author:** ![John\_WSLong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@John\_WSLong](https://www.ruby-forum.com/u/John_WSLong)\
**Post date:** [August 30, 2006, 4:09pm UTC](https://www.ruby-forum.com/t/overriding-class-methods-with-modules/66947/2 "2006-08-30T16:09:15Z")

</div>

John W. Long wrote:

> ```
> def my_method
> 
> ```
> 
> puts TestObject.another\_method #=\> “another method”
> 
> Why can’t I get TestObject to use my\_method from TestExtension?

Because we don’t have Cuts 😉

Okay a more presently practical answer. You can use a hack like:

```
module TestExtension
  def self.included(base)
    base.module_eval do
      def my_method
        "overridden by TestExtension"
      end
    end
  end
  def another_method
    "another method"
  end
end

```

T.

---

<div class="post-metadata">

**Author:** ![John\_WSLong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@John\_WSLong](https://www.ruby-forum.com/u/John_WSLong)\
**Post date:** [August 30, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/overriding-class-methods-with-modules/66947/3 "2006-08-30T16:45:21Z")

</div>

Trans wrote:

> ```
> def another_method
> "another method"
> end
> end
> 
> ```

Mmm. That doesn’t work either.

---

<div class="post-metadata">

**Author:** ![John\_WSLong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@John\_WSLong](https://www.ruby-forum.com/u/John_WSLong)\
**Post date:** [August 30, 2006, 4:53pm UTC](https://www.ruby-forum.com/t/overriding-class-methods-with-modules/66947/4 "2006-08-30T16:53:22Z")

</div>

On 8/30/06, John W. Long [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Trans wrote:
> 
> > Okay a more presently practical answer. You can use a hack like:
> > 
> > ```
> > module TestExtension
> > def self.included(base)
> > 
> > ```

shouldn’t be “extended” ?

---

<div class="post-metadata">

**Author:** ![John\_WSLong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@John\_WSLong](https://www.ruby-forum.com/u/John_WSLong)\
**Post date:** [August 30, 2006, 4:57pm UTC](https://www.ruby-forum.com/t/overriding-class-methods-with-modules/66947/5 "2006-08-30T16:57:22Z")

</div>

Hi –

On Wed, 30 Aug 2006, John W. Long wrote:

> def my\_method  
> puts TestObject.another\_method #=\> “another method”
> 
> Why can’t I get TestObject to use my\_method from TestExtension?

What you’re seeing is that in the method lookup path, a given class  
comes before any modules it includes. In your case, the class in  
question is the singleton class of TestObject: that class defines  
#my\_method, and also includes a module that defines #my\_method. The  
one defined in the class “wins”.

Here’s another, parallel example:

module M  
def my\_method  
puts “M#my\_method”  
end  
end

class C  
end

c = C.new

class \<\< c # defined the method in the class  
def my\_method  
puts “C#my\_method”  
end  
end

c.extend(M) # also include the module in the class  
c.my\_method # “C#my\_method” (the class wins)

The module would win if the method were defined in the _class_ of the  
object, rather than the singleton class – because then the lookup  
order would be:

singleton class (no)  
module included by singleton class (yes – execute method)  
[class (never reached)]

David

---

<div class="post-metadata">

**Author:** ![John\_WSLong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@John\_WSLong](https://www.ruby-forum.com/u/John_WSLong)\
**Post date:** [August 30, 2006, 5:19pm UTC](https://www.ruby-forum.com/t/overriding-class-methods-with-modules/66947/6 "2006-08-30T17:19:24Z")

</div>

That works. So the final code would be something like:

module TestExtension  
def inject\_methods(base)  
base.module\_eval do  
def self.my\_method  
“overridden by TestExtension”  
end  
end  
end

def extended(base)  
inject\_methods(base)  
end

def included(base)  
inject\_methods(base)  
end  
end

This has always been a little clunky in Ruby, primarily because  
methods written within a specific class were designed to override  
mixin and parent class methods, not vice-versa.
