[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Accessing base method...

Soso Soso

11/16/2006 4:17:00 PM

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

--
Posted via http://www.ruby-....

12 Answers

Miquel Oliete

11/16/2006 4:37:00 PM

0

On Fri, 17 Nov 2006 01:16:51 +0900
Soso Soso <soso_pub@yahoo.com> wrote:

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

Soso Soso

11/16/2006 4:41:00 PM

0

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

--
Posted via http://www.ruby-....

Stefano Crocco

11/16/2006 4:43:00 PM

0

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

--
Posted via http://www.ruby-....

Drew Olson

11/16/2006 4:45:00 PM

0

Soso Soso wrote:
> 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.

--
Posted via http://www.ruby-....

Soso Soso

11/16/2006 4:51:00 PM

0

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 Crocco wrote:

> If you want to be able to call a base class's method after having
> overriden it, you should use alias_method.

--
Posted via http://www.ruby-....

Vincent Fourmond

11/16/2006 4:52:00 PM

0

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


--
Vincent Fourmond, PhD student
http://vincent.fourmon...

Robert Klemme

11/16/2006 5:01:00 PM

0

On 16.11.2006 17:16, Soso Soso wrote:
> 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

Paul Lutus

11/16/2006 5:30:00 PM

0

Soso Soso wrote:

> 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()
-----------------------------------

All emit the same result.

--
Paul Lutus
http://www.ara...

Michael Perle

11/16/2006 6:10:00 PM

0

> Stefano Crocco wrote:
>
>>If you want to be able to call a base class's method after having
>>overriden it, you should use alias_method.

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

Rob Sanheim

11/16/2006 8:00:00 PM

0

On 11/16/06, Paul Lutus <nospam@nosite.zzz> wrote:
> Soso Soso wrote:
>
> > 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()
> -----------------------------------
>
> All emit the same result.
>
> --
> Paul Lutus
> http://www.ara...
>
>

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
--
http://www.robs...
http://www.seekin...
http://www.a...