Ruby程序不在屏幕上打印

时间:2016-03-30 13:12:51

标签: ruby

大家好日子。我正在编写一个简单的ruby代码,但它不会在屏幕上打印我使用ruby 2.2.2而我的IDE是Rubymine 7.提前谢谢。这是代码;

  
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}"    

1 个答案:

答案 0 :(得分: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}"