2015-05-24 62 views
0
class OrangeTree 
    def initialize(age = 0, height = 0) 
    @age = age 
    @height = height 
    @count = [] 
    end 

    def aging 
    until @age > 10 
     screening_array = Array.new 
     i = @age + 3 
     i.times do 
     screening_array << Orange.new 
     end 
     @age += 1 
    end 
    screening_array.each_with_index do |orange, index| 
     @count << orange if index.even? 
    end 
    p @count 
    end 
end 

class Orange 
    attr_accessor :diameter 
    # Initializes a new Orange with diameter +diameter+ 
    def initialize(diameter = rand(4) + 1) 
    @diameter = diameter 
    end 
end 

tree = OrangeTree.new 
tree.aging 

我試圖填補@count橙色具有指數3及以上..我可以得到最接近的是,即使指數得到橙色正從具體指標橙色..我該如何修改?從一個數組

+1

我只是做了你,但請,每個人的理智的緣故(包括你自己)下一次很好的格式化你的代碼! ;) – fny

回答

0

可以內嵌在比較你試圖讓:

screening_array.each_with_index do |orange, index| 
    @count << orange if index > 3 
end