Forum: Ruby inheritance question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
139a2a2f6b330cc59a8dc6384bbf8837?d=identicon&s=25 Corey Konrad (ckonrad)
on 2007-03-17 18:50
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}"
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2007-03-17 18:57
(Received via mailing list)
On 3/17/07, Corey Konrad <0011@hush.com> 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/
139a2a2f6b330cc59a8dc6384bbf8837?d=identicon&s=25 Corey Konrad (ckonrad)
on 2007-03-17 19:01
Rick Denatale wrote:
> On 3/17/07, Corey Konrad <0011@hush.com> 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")
139a2a2f6b330cc59a8dc6384bbf8837?d=identicon&s=25 Corey Konrad (ckonrad)
on 2007-03-17 19:03
Rick Denatale wrote:
> On 3/17/07, Corey Konrad <0011@hush.com> 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
85ae830f08b5ff5e39a1cbae9e3d0205?d=identicon&s=25 Olivier Renaud (Guest)
on 2007-03-17 19:37
(Received via mailing list)
Le samedi 17 mars 2007 18:50, Corey Konrad 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 :)
139a2a2f6b330cc59a8dc6384bbf8837?d=identicon&s=25 Corey Konrad (ckonrad)
on 2007-03-17 20:34
Olivier Renaud wrote:
> Le samedi 17 mars 2007 18:50, Corey Konrad 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.
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 Gary Wright (Guest)
on 2007-03-17 21:44
(Received via mailing list)
On Mar 17, 2007, at 3:34 PM, Corey Konrad 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 Wright
139a2a2f6b330cc59a8dc6384bbf8837?d=identicon&s=25 Corey Konrad (ckonrad)
on 2007-03-18 02:10
>
> 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 Wright

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.
This topic is locked and can not be replied to.