class Child < Parent
def knox
puts ‘child’
end
def test
knox # here I want to call knox method from Parent class… ??
super() # this is calling Parent same name method.
end
end
Thanks,
-soso
LLama Gratis a cualquier PC del Mundo.
Llamadas a fijos y móviles desde 1 céntimo por minuto. http://es.voice.yahoo.com
I know that, but if you look at the code carefully you will see that I
am trying to call base method ‘knox’ from inside ‘test’ method. So
parent() will try to call base method of ‘test’, which is not what I
want.
Thanks Stefano, it works. But it puzzles me (coming from a C++/Java
background) why isn’t there a more direct way to access an overriden
base class method. hmmm…
Stefano C. wrote:
If you want to be able to call a base class’s method after having
overriden it, you should use alias_method.
I’m new to ruby, being trying to access base class method but with no
luck until now. Here’s a snippet to get an idea:
class Parent
def knox
puts ‘parent’
end
end
class Child < Parent
def knox
puts ‘child’
end
def test
knox # here I want to call knox method from Parent class… ??
end
end
Thanks,
-soso
If you want to be able to call a base class’s method after having
overriden it, you should use alias_method. It’s an instance method of
class Module (and so it can be used within a class definition), which
makes a copy of the given method with a new name. You should use it this
way:
class Child < Parent
alias_method :parent_knox, :knox
def knox
puts ‘child’
end
def test
parent_knox
knox # here I want to call knox method from Parent class… ??
end
end
If you need to call the parent’s method in the method which is
overriding it, instead (in your case in the body of Child’s knox
method), you do so using the super keyword:
class Child <Parent
def knox
super
puts ‘child’
end
end
class Child < Parent
def knox
puts ‘child’
end
def test
knox # here I want to call knox method from Parent class… ??
end
end
I think this is not possible: when you redefine knox in the child, all
calls to knox go to the child method. However, you can use the parent’s
knox from whitin the child knox with super.
I know that, but if you look at the code carefully you will see that I
am trying to call base method ‘knox’ from inside ‘test’ method. So
parent() will try to call base method of ‘test’, which is not what I
want.
-soso
Can you explain your reasoning for this? I don’t believe this is
supported. Why would you use method overloading if you want to access
the parent’s method of the same name? Why not call your method in Child
child_knox, or something of the sort.
I know that, but if you look at the code carefully you will see that I
am trying to call base method ‘knox’ from inside ‘test’ method. So
parent() will try to call base method of ‘test’, which is not what I
want.
But the point of overriding in a subclass is to be able to use the same
method name to refer to a derived method. You appear to be working at
cross-purposes – on the one hand, redefining a method in the child
class,
on the other, trying to get around the fact that you have redefined it.
By the way, this works exactly the same way in Java and C++. If you
override
a method in the child class, you have to use “super” or some variant
thereof to access the parent’s version. Here are examples in C++, Java
and
Ruby, all of which emit the same result:
That java example is not analagous to what the OP wanted. Here is
what he wants, in java. Forgive syntax, its been awhile =).
class Dog {
public String speak() { return “bark”; }
}
class Pug extends Dog {
public String speak() { return “snort snort bark”; }
public String barkLikeNormalDog() { return super.speak(); }
}
There is no way to do this with the super keyword in Ruby AFAIK.
Super in Java basically gives you a reference to the instance of the
superclass. In Ruby, its a bit more magical - to quote the pickaxe:
“…instead it is an executable statement that reinvokes the current
method, skipping any definition in the class of the current object.”
You need to use some of the other tricks that have been posted already.
If you want to be able to call a base class’s method after having
overriden it, you should use alias_method.
Soso S. wrote (and Michael corrected the quoting):
Thanks Stefano, it works. But it puzzles me (coming from a C++/Java
background) why isn’t there a more direct way to access an overriden
base class method. hmmm… >
How do you do this in Java then?
Are you sure that you really want to overwrite
it and then use the parent one? Why did you
overwrite it then?
class Parent
def knox
puts ‘parent’
end
end
class Child < Parent
def knox
puts ‘child’
end
def test
Parent.new.knox
end
end
But do not think that it contributes to the readability of
the application to have 2 methods with the same name that
do two different things at all. So perhaps I would go for:
class Parent
def knox_parent
puts ‘parent’
end
end
class Child < Parent
def knox_child
puts ‘child’
end
def test
knox_parent()
end
end
MP
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.