# Inheritance question

**URL:** <https://www.ruby-forum.com/t/inheritance-question/89136>\
**Category:** Ruby\
**Created:** [March 17, 2007, 6:50pm UTC](https://www.ruby-forum.com/t/inheritance-question/89136 "2007-03-17T18:50:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [March 17, 2007, 6:50pm UTC](https://www.ruby-forum.com/t/inheritance-question/89136/1 "2007-03-17T18:50:35Z")

</div>

I am trying to figure out how inheritance works using the super method.  
With the sample code below i get the error that i dont have enough  
arguments for the constructor i dont understand why i am getting that  
error.

class Animal  
def initialize(color, age)  
@color = color  
@age = age  
end

attr\_accessor :color  
attr\_accessor :age  
end

class Dog \< Animal  
def initialize(color, age, sound)  
super(color)  
super(age)  
@sound = sound  
end

attr\_accessor :sound  
end

dog = Dog.new(“red”, 25, “Bark”)  
puts “The new dog is #{dog.color}”  
puts “The age of the dog is #{dog.age}”  
puts “The new dog says #{dog.sound}”

---

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [March 17, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/inheritance-question/89136/2 "2007-03-17T18:57:40Z")

</div>

On 3/17/07, Corey K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am trying to figure out how inheritance works using the super method.  
> With the sample code below i get the error that i dont have enough  
> arguments for the constructor i dont understand why i am getting that  
> error.

> class Dog \< Animal  
> def initialize(color, age, sound)  
> super(color,age)  
> @sound = sound  
> end

Animals initialize method has two arguments.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [March 17, 2007, 7:01pm UTC](https://www.ruby-forum.com/t/inheritance-question/89136/3 "2007-03-17T19:01:19Z")

</div>

Rick Denatale wrote:

> On 3/17/07, Corey K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I am trying to figure out how inheritance works using the super method.  
> > With the sample code below i get the error that i dont have enough  
> > arguments for the constructor i dont understand why i am getting that  
> > error.
> 
> > class Dog \< Animal  
> > def initialize(color, age, sound)  
> > super(color,age)  
> > @sound = sound  
> > end
> 
> Animals initialize method has two arguments.
> 
> –  
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

i Know but i initialized all of them in this statement though I  
initialized the 2 in the super class and then the 1 in the subclass:  
dog = Dog.new(“red”, 25, “Bark”)

---

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [March 17, 2007, 7:37pm UTC](https://www.ruby-forum.com/t/inheritance-question/89136/4 "2007-03-17T19:37:48Z")

</div>

Le samedi 17 mars 2007 18:50, Corey K. a Ã©critÂ :

> I am trying to figure out how inheritance works using the super method.  
> With the sample code below i get the error that i dont have enough  
> arguments for the constructor i dont understand why i am getting that  
> error.  
> […]

Hi Corey,

I have read the questions you posted the last few days, and I would like  
to  
give you a general counsel. It looks like you don’t take care of the  
output  
when an exception is raised. Though, the informations displayed when an  
error  
occurs are very useful, and if you had paid attention to it, you would  
have  
found your error immediatly.

For your particular problem, you can see this output :

/tmp/animal.rb:13:in `initialize': wrong number of arguments (1 for 2) (ArgumentError) from /tmp/animal.rb:13:in`initialize’  
from /tmp/animal.rb:21:in `new’  
from /tmp/animal.rb:21

If you don’t already know, the first line means that at line 13 (line 13  
is :  
super(color)) a method was called with 1 argument instead of 2. This is  
sufficient to start tracking down the error. The other lines represents  
the  
call stack, ie the “path” which was followed to reach the exception.

And if there is an exception you don’t understand, or that doesn’t help  
you,  
then join it with your code in your next message.

Happy learning 🙂

---

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [March 17, 2007, 8:34pm UTC](https://www.ruby-forum.com/t/inheritance-question/89136/5 "2007-03-17T20:34:24Z")

</div>

Olivier R. wrote:

> Le samedi 17 mars 2007 18:50, Corey K. a Ã©crit :
> 
> > I am trying to figure out how inheritance works using the super method.  
> > With the sample code below i get the error that i dont have enough  
> > arguments for the constructor i dont understand why i am getting that  
> > error.  
> > […]
> 
> Hi Corey,
> 
> I have read the questions you posted the last few days, and I would like  
> to  
> give you a general counsel. It looks like you don’t take care of the  
> output  
> when an exception is raised. Though, the informations displayed when an  
> error  
> occurs are very useful, and if you had paid attention to it, you would  
> have  
> found your error immediatly.
> 
> For your particular problem, you can see this output :
> 
> /tmp/animal.rb:13:in `initialize': wrong number of arguments (1 for 2) (ArgumentError) from /tmp/animal.rb:13:in`initialize’  
> from /tmp/animal.rb:21:in `new’  
> from /tmp/animal.rb:21
> 
> If you don’t already know, the first line means that at line 13 (line 13  
> is :  
> super(color)) a method was called with 1 argument instead of 2. This is  
> sufficient to start tracking down the error. The other lines represents  
> the  
> call stack, ie the “path” which was followed to reach the exception.
> 
> And if there is an exception you don’t understand, or that doesn’t help  
> you,  
> then join it with your code in your next message.
> 
> Happy learning 🙂

Alright thanks for the information. I knew what the error message meant  
to some degree i knew it had to do with the number of args in the  
constructor and i knew what line it was on but i still didnt understand  
why it was happening because i didnt understand how to use the super  
method correctly, i looked it up doing a ri super and there was no info  
that shed light on the issue either so i asked on here. The error  
message didnt help me out at all i mean if it spit out “Hey corey you  
need to do this super(color, age) instead of super(color) super(age)”  
then i would have had no problems understanding that.

---

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [March 17, 2007, 9:44pm UTC](https://www.ruby-forum.com/t/inheritance-question/89136/6 "2007-03-17T21:44:59Z")

</div>

On Mar 17, 2007, at 3:34 PM, Corey K. wrote:

> i still didnt understand  
> why it was happening because i didnt understand how to use the super  
> method correctly, i looked it up doing a ri super and there was no  
> info  
> that shed light on the issue either so i asked on here.

Your thought to look at ‘ri super’ makes sense. Unfortunately, ‘super’  
isn’t actually a method, although it looks like one. It is actually  
a keyword that is part of the language syntax in the same way that  
‘class’ and ‘end’ are in:

```
class Animal
end

```

The ‘ri’ tool is only helpful in looking up actual methods and  
doesn’t really help at all with the underlying language structure  
and semantics. Other things that look like methods but that you  
won’t find explained via ‘ri’ are:

yield  
next  
break  
return

and so on. Best to use the index of the Pickaxe or Ruby Way or some  
other Ruby reference for these types of things.

I wonder if it might make sense to have the standard Ruby documentation  
include basic information on keywords and incorporate that into  
the ri/rdoc documentation set.

Gary W.

---

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [March 17, 2007, 7:03pm UTC](https://www.ruby-forum.com/t/inheritance-question/89136/7 "2007-03-17T19:03:41Z")

</div>

Rick Denatale wrote:

> On 3/17/07, Corey K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I am trying to figure out how inheritance works using the super method.  
> > With the sample code below i get the error that i dont have enough  
> > arguments for the constructor i dont understand why i am getting that  
> > error.
> 
> > class Dog \< Animal  
> > def initialize(color, age, sound)  
> > super(color,age)  
> > @sound = sound  
> > end
> 
> Animals initialize method has two arguments.
> 
> –  
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

oh you just use 1 super method for all the args in the super class’s  
constructor, i was doing

super(color)  
super(age)

but its just supposed to be

super(color, age)

ok thanks

---

<div class="post-metadata">

**Author:** ![ckonrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@ckonrad](https://www.ruby-forum.com/u/ckonrad)\
**Post date:** [March 18, 2007, 2:10am UTC](https://www.ruby-forum.com/t/inheritance-question/89136/8 "2007-03-18T02:10:55Z")

</div>

> The ‘ri’ tool is only helpful in looking up actual methods and  
> doesn’t really help at all with the underlying language structure  
> and semantics. Other things that look like methods but that you  
> won’t find explained via ‘ri’ are:
> 
> yield  
> next  
> break  
> return
> 
> and so on. Best to use the index of the Pickaxe or Ruby Way or some  
> other Ruby reference for these types of things.
> 
> I wonder if it might make sense to have the standard Ruby documentation  
> include basic information on keywords and incorporate that into  
> the ri/rdoc documentation set.
> 
> Gary W.

oh ok thanks i didnt know that either, i am learning alot of stuff today  
if i keep at this pace for awhile i should be up and running with ruby  
on rails in no time and then i can figure out what to do with it.
