Reputation: 11
Good day everyone. I am writing a simple ruby code but it doesn't print on the screen I am using ruby 2.2.2 and my IDE is Rubymine 7.Thanks in advance.Here is the code;
class Animal
attr_accessor :name, :colour
def initialize(name,colour)
@name = name
@colour = colour
end
def to_s
"Name#{@name} Colour #{@colour}"
end
end
class Cheetah < Animal
attr_reader :speed
def initialize(name,colour,speed)
@speed = speed
super (name,colour)
end
def to_s
return super + "Speed#{@speed}kph"
end
end
class Zoo < Animal
def initialize
@animals = []
end
def add_animals(animal)
@animals << animal
end
def my_animals
cage = ""
@animal.each do|call|
cage += call.to_s
end
end
end
5.times do|count|
Zoo.add_animals(MyAnimal.new("Cheetah#{count}","yello and black spots"))
end
puts "#{Zoo.my_animals}"
My_cheetah = Cheetah.new("Cheetah","yello and black spots",180)
puts "#{My_cheetah.to_s}"
Upvotes: 0
Views: 67
Reputation: 3053
class Animal
attr_accessor :name, :colour
def initialize(name,colour)
@name = name
@colour = colour
end
def to_s
"Name#{@name} Colour #{@colour}"
end
end
class Cheetah < Animal
attr_reader :speed
def initialize(name,colour,speed)
@speed = speed
super(name,colour)
end
def to_s
return super + "Speed#{@speed}kph"
end
end
class Zoo < Animal
def initialize
@animals = []
end
def add_animal(animal)
@animals << animal
end
def my_animals
cage = ""
@animals.each do |call|
cage += call.to_s
end
cage
end
end
zoo = Zoo.new
5.times do|count|
zoo.add_animal(Animal.new("Cheetah#{count}","yello and black spots"))
end
puts "#{zoo.my_animals}"
my_cheetah = Cheetah.new("Cheetah","yello and black spots",180)
puts "#{my_cheetah.to_s}"
Upvotes: 1