Accessing base method...

S

Soso Soso

Hi all,

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
 
M

Miquel

Hi all,

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

Soso Soso

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
 
S

Stefano Crocco

Soso said:
Hi all,

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

I hope this helps

Stefano
 
D

Drew Olson

Soso said:
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.
 
S

Soso Soso

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...
 
V

Vincent Fourmond

Soso said:
Hi all,

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

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.

Vince
 
R

Robert Klemme

Hi all,

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

use "super".

robert
 
M

Michael Perle

Soso Soso 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
 
R

Rob Sanheim

Soso said:
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:

C++:
-----------------------------------

#include <iostream>

using namespace std;

class Parent {
public:
void try_this(void) {
cout << "parent" << endl;
}
};

class Child : public Parent {
public:
void try_this(void) {
Parent::try_this();
cout << "child" << endl;
}
};

int main(int argc, char **argv) {
Child ch;
ch.try_this();
return 0;
}
-----------------------------------

Java:
-----------------------------------

class Parent {
void try_this() {
System.out.println("parent");
}
}

public class Parent_Child extends Parent {
void try_this() {
super.try_this();
System.out.println("child");
}
public static void main(String args[]) {
Parent_Child ch = new Parent_Child();
ch.try_this();
}
};
-----------------------------------

Ruby:
-----------------------------------

#!/usr/bin/ruby -w

class Parent
def try_this()
puts "parent"
end
end

class Child < Parent
def try_this()
super()
puts "child"
end
end

ch = Child.new
ch.try_this()

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.

- Rob
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Accessing base method..."

|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

Prepare alias for the parent knox, or use instance_method-bind-call
hack.

class Parent
def knox
puts 'parent'
end
alias knox_parent
end

class Child < Parent
def knox
puts 'child'
end
def test
knox_parent
# -or-
Parent.instance_method:)knox).bind(self).call
end
end


matz.
 
T

Trans

Soso said:
Hi all,

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:

require 'facets'
require 'kernel/as'
class Parent
def knox
puts 'parent'
end
end

class Child < Parent
def knox
puts 'child'
end
def test as(Parent).knox
end
end

T.
 

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

No members online now.

Forum statistics

Threads
474,219
Messages
2,571,117
Members
47,729
Latest member
taulaju99

Latest Threads

Top