0

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}"    
2
  • In the definition of Zoo.my_animals you're referencing @animal (i.e. not pluralized), also in that same method, you're building up a string (cage), but you never return it (the return value of a call to .each is nil). Commented Mar 30, 2016 at 13:25
  • 1
    The return value of each is self, as specified in the documentation of each. Commented Mar 30, 2016 at 13:28

1 Answer 1

1
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}"
Sign up to request clarification or add additional context in comments.

4 Comments

Rather than a code dump, indicate what needs to be changed, and why.
@DaveNewton That is absolutely right. In particular case the main need is to do some research by yourself rather than post such code on SO.
@MaximPontyushenko hey Maxim thanx plus everyone for the reply, I copied and pasted your code but unfortunately it also didn't print on screen.
@AllanBast It means you did not copy-paste correctly or did not run program correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.