C
Corey Konrad
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}"
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}"