calling child methods in parent

D

Dave Ru

------=_Part_7982_13889138.1139360052255
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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 =3D 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.

------=_Part_7982_13889138.1139360052255--
 
M

Matthew Smillie

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top