# Help on inheritance and class methods pls?

**URL:** <https://www.ruby-forum.com/t/help-on-inheritance-and-class-methods-pls/151147>\
**Category:** Ruby\
**Created:** [October 17, 2008, 11:54am UTC](https://www.ruby-forum.com/t/help-on-inheritance-and-class-methods-pls/151147 "2008-10-17T11:54:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bemmi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/278dde/32.png) [@bemmi](https://www.ruby-forum.com/u/bemmi)\
**Post date:** [October 17, 2008, 11:54am UTC](https://www.ruby-forum.com/t/help-on-inheritance-and-class-methods-pls/151147/1 "2008-10-17T11:54:37Z")

</div>

Hi there,

Hoping someone explain this to me as I’d like to understand it properly.  
I’ve been reading about Ruby’s metaclasses, but I don’t get this:

class Test  
def foo  
puts “in foo”  
end  
end  
=\> nil

class Test2 \< Test  
def self.bar  
foo  
end  
def method\_missing(name, \*args)  
puts “in method\_missing”  
end  
end  
=\> nil

> Test2.new.bar  
> in method\_missing  
> =\> nil  
> Test2.new.foo  
> in foo  
> =\> nil

why does invoking method bar on the instance find class Test2’s  
method\_missing instance method, but not instance method foo inherited  
from class Test?

Thanks for any help!

---

<div class="post-metadata">

**Author:** ![bemmi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/278dde/32.png) [@bemmi](https://www.ruby-forum.com/u/bemmi)\
**Post date:** [October 17, 2008, 12:41pm UTC](https://www.ruby-forum.com/t/help-on-inheritance-and-class-methods-pls/151147/2 "2008-10-17T12:41:31Z")

</div>

Hi –

On Fri, 17 Oct 2008, Barbara Mcinnes wrote:

> =\> nil
> 
> > Test2.new.bar  
> > in method\_missing  
> > =\> nil  
> > Test2.new.foo  
> > in foo  
> > =\> nil
> 
> why does invoking method bar on the instance find class Test2’s  
> method\_missing instance method, but not instance method foo inherited  
> from class Test?

You’ve got a class method Test2.bar, but you never call it. (The  
program would run the same if you deleted that method.) When you send  
the message ‘bar’ to your instance of Test2, it hits method\_missing  
because there’s no instance method bar in Test2 or any of its  
ancestors.

David
