Maerus
Maerus

Reputation: 113

Ruby: Wrong number of Arguments for a subclass changes number of arguments

I am very new to Ruby and I have been looking for an answer to my question, but haven't found an answer yet. This is my code:

class Animal
  def initialize(aName, anAge)
    @name = aName
    @age = anAge
  end
end

class Cat < Animal
  def initialize(aName, anAge, aBreed)
    @breed = aBreed
    super(aName, anAge, aBreed)
  end
end

When I try to create a new cat object with defining three parameters, it says: ArgumentError: Wrong number of Arguments (3 for 2). But when I do the same thing with two parameters I get (2 for 3).

I can't seem to figure it out... Thanks for your help!

Upvotes: 1

Views: 380

Answers (1)

usha
usha

Reputation: 29369

Your super class Animal constructor takes only two parameters aName and anAge. so you should only pass first two arguments of Cat to Animal.

class Cat < Animal
  def initialize(aName, anAge, aBreed)
    @breed = aBreed
    super(aName, anAge)
  end
end

Upvotes: 3

Related Questions