[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

help on inheritance and class methods pls?

Barbara Mcinnes

10/17/2008 9:54:00 AM

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!
--
Posted via http://www.ruby-....

1 Answer

David A. Black

10/17/2008 10:40:00 AM

0

Hi --

On Fri, 17 Oct 2008, Barbara Mcinnes wrote:

> 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?

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

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.r... for details and updates!