[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

calling child methods in parent

Dave Ru

2/8/2006 12:55:00 AM

Hi-
This may be a pretty easy question but I'm new to ruby

Say I have a class like this

class MyClass

def myCallingMethod
myMethodToOverrider("hello")
end

def myMethodToOverride(string)

end
end

and I want to create a chile method like so

class ChildClass < MyClass
def myMethodToOverride(string)
puts string
end
end

Then I create an instance of my child class

child = ChildClass.new()

then I call myCallingMethod

child.myCallingMethod()

is there anyway to have the method in the parent object call the overriden
method in the child?

Thanks in advance.
2 Answers

Matthew Smillie

2/8/2006 2:21:00 AM

0

> is there anyway to have the method in the parent object call the
> overriden
> method in the child?

That's the default behaviour, actually. In C++ terms, all methods
are virtual.

Observe:

class Parent
def say_what
virtual_method("Hello World")
end
def virtual_method(str)
"I say: #{str}"
end
end

class Child < Parent
def virtual_method(str)
"My parent says: #{str}"
end
end

william = Parent.new
william.say_what
# => "I say: Hello World"

billy = Child.new
billy.say_what
# => "My parent says: Hello World"

matthew smillie.



Ron Hamilton

8/11/2009 4:52:00 PM

0

stupid *impotent* cheeselog lied:

> On Mon, 10 Aug 2009 20:18:08 -0700, Wilson Woods
> <banmilk@hotmail.com> wrote:
>
>>> Concession of defeat noted and accepted.
>>>
>>> Being laughed at
>> You conceded defeat.
>
> No, you merely claimed I did

No, you did: you conceded defeat when you started your fearful editing.