Inheritance question

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}”

On 3/17/07, Corey K. [email protected] 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/

Rick Denatale wrote:

On 3/17/07, Corey K. [email protected] 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/

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

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:ininitialize’
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 :slight_smile:

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:ininitialize’
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 :slight_smile:

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.

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.

Rick Denatale wrote:

On 3/17/07, Corey K. [email protected] 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/

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

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.