# Access to method in superclass

**URL:** <https://www.ruby-forum.com/t/access-to-method-in-superclass/146815>\
**Category:** Ruby\
**Created:** [August 25, 2008, 8:39pm UTC](https://www.ruby-forum.com/t/access-to-method-in-superclass/146815 "2008-08-25T20:39:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [August 25, 2008, 8:39pm UTC](https://www.ruby-forum.com/t/access-to-method-in-superclass/146815/1 "2008-08-25T20:39:18Z")

</div>

LIKE ACCEDING TO THE METHOD " setSpeed" OF THE SUPERCLASS “Airplane”.  
Thak you

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [August 26, 2008, 2:15am UTC](https://www.ruby-forum.com/t/access-to-method-in-superclass/146815/2 "2008-08-26T02:15:25Z")

</div>

On Aug 25, 2:35 pm, David C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> LIKE ACCEDING TO THE METHOD " setSpeed" OF THE SUPERCLASS “Airplane”.  
> Thak you
> 
> Attachments:[http://www.ruby-forum.com/attachment/2606/prueba1.rb](http://www.ruby-forum.com/attachment/2606/prueba1.rb)
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

Hi David,

Your access to Airplane::setSpeed is working.

On line 55, you create an Airplane::Jet instance with @speed=0  
On line 56, you invoke that setSpeed on the instance with argument 422  
so the method on line 38 is called with 422  
which in turn invokes Airplane::setSpeed with the argument  
422\*Multiplier, of 844,  
which is then set in @speed of the Airplane::Jet instance  
On line 57, you print out the 844

Then on line 57, you invoke

Line 56 set the Jet’s speed to 0  
Line 57 with argument 422 set the Jet’s speed to 422\*Multiplier, or  
844 and printed it

Line 60 invoked accelerate, which in turn invoked  
Line 45 setSpeed(getSpeed()\*2)  
getSpeed returned your 844, which then got doubled to 1688  
which was used as the argument to setSpped, which applied the  
Multiplier  
so the speed was set to 3376  
Line 61 used getSpeed to retrieve the 3376, which then got printed.

All this is confirmed by lines 2 and 2 of your output.

HTH,  
Richard

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [August 26, 2008, 5:23pm UTC](https://www.ruby-forum.com/t/access-to-method-in-superclass/146815/3 "2008-08-26T17:23:52Z")

</div>

RichardOnRails wrote:

> On Aug 25, 2:35 pm, David C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > LIKE ACCEDING TO THE METHOD " setSpeed" OF THE SUPERCLASS “Airplane”.  
> > Thak you
> > 
> > Attachments:[http://www.ruby-forum.com/attachment/2606/prueba1.rb](http://www.ruby-forum.com/attachment/2606/prueba1.rb)
> > 
> > –  
> > Posted viahttp://www.ruby-forum.com/.
> 
> Hi David,
> 
> Your access to Airplane::setSpeed is working.
> 
> On line 55, you create an Airplane::Jet instance with @speed=0  
> On line 56, you invoke that setSpeed on the instance with argument 422  
> so the method on line 38 is called with 422  
> which in turn invokes Airplane::setSpeed with the argument  
> 422\*Multiplier, of 844,  
> which is then set in @speed of the Airplane::Jet instance  
> On line 57, you print out the 844
> 
> Then on line 57, you invoke
> 
> Line 56 set the Jet’s speed to 0  
> Line 57 with argument 422 set the Jet’s speed to 422\*Multiplier, or  
> 844 and printed it
> 
> Line 60 invoked accelerate, which in turn invoked  
> Line 45 setSpeed(getSpeed()\*2)  
> getSpeed returned your 844, which then got doubled to 1688  
> which was used as the argument to setSpped, which applied the  
> Multiplier  
> so the speed was set to 3376  
> Line 61 used getSpeed to retrieve the 3376, which then got printed.
> 
> All this is confirmed by lines 2 and 2 of your output.
> 
> HTH,  
> Richard

class Jet \< Airplane  
MULTIPLIER = 2  
def initialize  
super  
end  
def setSpeed(speed)  
super(speed\*MULTIPLIER)  
end  
def accelerate  
# TROUBLE!!!  
# LIKE ACCEDING TO THE METHOD " setSpeed"  
# OF THE SUPERCLASS “Airplane”  
Airplane::setSpeed(getSpeed()\*2)  
end

end

At the time of making it run it leaves the error

> ruby prueba1.rb  
> prueba1.rb:28:in `accelerate': undefined method `setSpeed’ for  
> Airplane:Class (NoMethodError)  
> from prueba1.rb:43:in `principal’  
> from prueba1.rb:57  
> 212  
> 844  
> Exit code: 1

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [August 28, 2008, 9:20pm UTC](https://www.ruby-forum.com/t/access-to-method-in-superclass/146815/4 "2008-08-28T21:20:29Z")

</div>

Hi RichardOnRails, thank you very much  
Recently I’m learning this language

Ok  
Version is  
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]  
Operating System is Windows 2003 server (english)

The aim is to bring the code in this java to ruby exactly like this for  
learning purposes.

// The code was drawn from the book “Head First Object Oriented Analysis  
and Design”  
import java.util.\*;  
class Airplane {  
private int speed;

public Airplane() {  
}

public void setSpeed(int speed) {  
this.speed = speed;  
}  
public int getSpeed() {  
return speed;  
}  
}

class Jet extends Airplane {  
private static final int MULTIPLIER = 2;  
public Jet() {  
super();  
}

public void setSpeed(int speed) {  
super.setSpeed(speed \* MULTIPLIER);  
}

```
    // HERE STAY MY PROBLEM!!!!!!!!!
    public void accelerate() {
// Access to method "setSpeed" of class Airplane
super.setSpeed(getSpeed() * 2);

```

}  
}  
class FlyTest {  
public static void main(String[] args) {  
Airplane biplane = new Airplane();  
biplane.setSpeed(212);  
System.out.println(biplane.getSpeed());  
Jet boeing = new Jet();  
boeing.setSpeed(422);  
System.out.println(boeing.getSpeed());  
int x = 0;  
while (x\<4) {  
boeing.accelerate();  
System.out.println(boeing.getSpeed());  
if (boeing.getSpeed()\>5000) {  
biplane.setSpeed(biplane.getSpeed() \* 2);  
} else {  
boeing.accelerate();  
}  
x++;  
}  
System.out.println(biplane.getSpeed());  
}  
}

The output is expected is:  
212  
844  
1688  
6752  
13504  
27008  
1696

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [August 28, 2008, 3:15am UTC](https://www.ruby-forum.com/t/access-to-method-in-superclass/146815/5 "2008-08-28T03:15:56Z")

</div>

On Aug 26, 11:19 am, David C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Hi David,
> 
> > Multiplier  
> > def initialize  
> > end  
> > 844\>Exit code: 1
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

Hi David,

I can’t see what’s wrong on your program/machine.

I ran the code you posted unchanged and got the following output:  
212  
844  
3376  
54016  
216064  
864256  
1696

The first question I’d ask is, what version of Ruby/OS are you  
running … though I doubt that will be too helpful? I’m running:  
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]  
WinXP-Pro/SP2

The second question is, are you up to cleaning up code and debugging  
it? At [http://www.pastie.org/261401](http://www.pastie.org/261401), I posted a version of your  
program that is closer to what I understand to be “The Ruby Way”. I  
don’t understand your purpose, so I can’t do anymore. I hope this  
version runs on your machine as it does on mine. I also hope it leads  
to a clearer understanding of your program flow.

The version I posted doesn’t produce the same numbers that your  
original did, but I don’t want to spend anymore time on it. As I  
said, my goal is to help you get closer to “The Ruby Way.”

Best wishes,  
Richard

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [August 28, 2008, 9:32pm UTC](https://www.ruby-forum.com/t/access-to-method-in-superclass/146815/6 "2008-08-28T21:32:44Z")

</div>

Hello.

It’s a gotcha in Ruby - there’s NO WAY to access ancestor’s method  
setSpeed from your own method accelerate. As you see, the word super  
itself is a call to ancestor’s method of the same name as the method you  
are currently in. And there’s no other use of super, so you cannot call  
ancestor’s (overridden) method of a different name than the method  
you’re actually in.

AFAIK they say that you shouldn’t need to. If you do, you can refactor  
your code and it will be a good thing to do.

TPR.

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [August 28, 2008, 10:24pm UTC](https://www.ruby-forum.com/t/access-to-method-in-superclass/146815/7 "2008-08-28T22:24:26Z")

</div>

Thomas B. wrote:

> Hello.
> 
> It’s a gotcha in Ruby - there’s NO WAY to access ancestor’s method  
> setSpeed from your own method accelerate. As you see, the word super  
> itself is a call to ancestor’s method of the same name as the method you  
> are currently in. And there’s no other use of super, so you cannot call  
> ancestor’s (overridden) method of a different name than the method  
> you’re actually in.
> 
> AFAIK they say that you shouldn’t need to. If you do, you can refactor  
> your code and it will be a good thing to do.
> 
> TPR.

Thank you very much. Then continuous learning â€¦

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [August 30, 2008, 4:04am UTC](https://www.ruby-forum.com/t/access-to-method-in-superclass/146815/8 "2008-08-30T04:04:54Z")

</div>

On Aug 28, 3:15 pm, David C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> this.speed = speed;
> super();
> 
> ```
> 
> }}  
> while (x\<4) {  
> }  
> 1696
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

Hi David,

OK. I understand your problem. You did the natural thing: you  
worked hard to convert the Java program directly into a Ruby program.  
It was natural, but it was the wrong approach. Nowadays, the  
emphasis in programming is Agile Programming and Test-Driven or  
Behavior-Driven Programming. That focuses on writing code in small  
pieces and making sure they work correctly. Then add more code, and  
make sure that’s right.

So the approach I recommend is:

• Make sure the Java code works as advertised. Check out “Test  
FlyTest.java” at [http://www.pastie.org/pastes/262610](http://www.pastie.org/pastes/262610)  
• Then I show you how to take the first step in “FlyTest1.rb” at  
[http://www.pastie.org/pastes/262618](http://www.pastie.org/pastes/262618)  
• Ditto for the second step in “FlyTest2.rb” at  
[http://www.pastie.org/pastes/262622](http://www.pastie.org/pastes/262622)  
• Ditto for the third step in “FlyTest3.rb” at  
[http://www.pastie.org/pastes/262624](http://www.pastie.org/pastes/262624)

In each of the Ruby code I include the Java code in a couple of  
comments. I do that for convenience as I try to replicate the Java  
functionality in Ruby code. If you don’t like that, you could just as  
easily keep the Java code open in a separate window.

As you add code try to make it good Ruby code, nor “Rubyized” Java.  
If you don’t know how to express something in Ruby, look at the code  
I originally sketched out for you or post a narrow question on the  
newsgroup. I recommend you do that rather than posting a lot of code.

For more guidance, check out “Agile Ruby”, “Test-Drive Development  
(TDD)” and “Behavior-Driven Development (BDD)” in Google. That’ll  
give you some more guidance.

Good luck.

HTH,  
Richard

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [September 2, 2008, 12:49am UTC](https://www.ruby-forum.com/t/access-to-method-in-superclass/146815/9 "2008-09-02T00:49:37Z")

</div>

ok, see you later!!! thk
