Forum: Ruby help on inheritance and class methods pls?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A7d7f5db5a4f72544d08913bcd3d4514?d=identicon&s=25 Barbara Mcinnes (bemmi)
on 2008-10-17 11:54
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!
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2008-10-17 12:41
(Received via mailing list)
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
This topic is locked and can not be replied to.