# Mixing-in the inherited method

**URL:** <https://www.ruby-forum.com/t/mixing-in-the-inherited-method/60594>\
**Category:** Ruby\
**Created:** [May 15, 2006, 11:30pm UTC](https://www.ruby-forum.com/t/mixing-in-the-inherited-method/60594 "2006-05-15T23:30:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christoffer\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@Christoffer\_S](https://www.ruby-forum.com/u/Christoffer_S)\
**Post date:** [May 15, 2006, 11:30pm UTC](https://www.ruby-forum.com/t/mixing-in-the-inherited-method/60594/1 "2006-05-15T23:30:07Z")

</div>

Dear Rubyists,

I fail to understand why the following code doesn’t output anything.

module Foobar  
def inherited(klass)  
p klass  
end  
end

class Class  
include Foobar  
end

class A  
end

My intuition says it should work since defining Class#inherited  
directly works. I believe I understand the whole “instance methods of  
Class become class methods of Class instances” business, but I must be  
missing something obvious.

Any ideas?

---

<div class="post-metadata">

**Author:** ![Christoffer\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@Christoffer\_S](https://www.ruby-forum.com/u/Christoffer_S)\
**Post date:** [May 16, 2006, 12:23am UTC](https://www.ruby-forum.com/t/mixing-in-the-inherited-method/60594/2 "2006-05-16T00:23:34Z")

</div>

Christoffer S. wrote:

> class Class
> 
> Any ideas?

A is not a subclass of Class, it’s an instance of Class.

Also, you need to use extend rather than include, so that the instance  
methods of Foobar (particularly, #inherited) become methods of A.  
Otherwise, the instance methods of Foobar become instance methods of  
_instances_ of A.

These two examples both print “A”:

module Foobar  
def inherited(klass)  
p klass  
end  
end

class Object  
extend Foobar  
end

class A  
end

* * *

module Foobar  
def inherited(klass)  
p klass  
end  
end

class Classy  
extend Foobar  
end

class A \< Classy  
end

---

<div class="post-metadata">

**Author:** ![Christoffer\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@Christoffer\_S](https://www.ruby-forum.com/u/Christoffer_S)\
**Post date:** [May 19, 2006, 5:35pm UTC](https://www.ruby-forum.com/t/mixing-in-the-inherited-method/60594/3 "2006-05-19T17:35:20Z")

</div>

Hello Joel,

> A is not a subclass of Class, it’s an instance of Class.

Indeed.

> Also, you need to use extend rather than include, so that the instance  
> methods of Foobar (particularly, #inherited) become methods of A.  
> Otherwise, the instance methods of Foobar become instance methods of  
> _instances_ of A.
> 
> These two examples both print “A”:
> 
> /…/

I understand your examples and your argument, but you seem to be  
missing my point (if I have one). Instance methods of class Class  
become class methods of Class instances, so why doesn’t the mixed-in  
inherited() work? Comparing these two examples, why is the output of  
the second different?

= Example 1

class Class  
def inherited(klass)  
p klass  
end

def test!  
p “test!”  
end  
end

class A; end  
A.test!

== Output

A  
“test!”

= Example 2

module Foo  
def inherited(klass)  
p klass  
end

def test!  
p “test!”  
end  
end

class Class  
include Foo  
end

class A; end  
A.test!

== Output

“test!”

Thanks,

---

<div class="post-metadata">

**Author:** ![Christoffer\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@Christoffer\_S](https://www.ruby-forum.com/u/Christoffer_S)\
**Post date:** [May 19, 2006, 7:26pm UTC](https://www.ruby-forum.com/t/mixing-in-the-inherited-method/60594/4 "2006-05-19T19:26:16Z")

</div>

> > > > > “C” == Christoffer S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

Try this

C\> class Class

```
 remove_method :inherited

```

C\> include Foo  
C\> end

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Christoffer\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@Christoffer\_S](https://www.ruby-forum.com/u/Christoffer_S)\
**Post date:** [May 19, 2006, 11:51pm UTC](https://www.ruby-forum.com/t/mixing-in-the-inherited-method/60594/5 "2006-05-19T23:51:45Z")

</div>

Guy Decoux wrote:

> Try this
> 
> C\> class Class
> 
> ```
> remove_method :inherited
> 
> ```
> 
> C\> include Foo  
> C\> end

Aha! That makes sense.

Thanks,
