我已經爲桃樹定義了一個類作爲任務的一部分。我想知道是否可以在我的一種方法中包含一個if語句,以使樹在60年後死亡。這是我的代碼:可以在ruby中的方法中包含if語句嗎?
class Tree
def initialize(color="green", fruit="peaches", age=0, peaches_amount=0)
@color = color
@fruit = fruit
@age = age
end
#age increases by one year every year
def the_age
@age += 1
return @age
end
#yield of peaches increases by 5 peaches every year
def peaches
@peaches_amount += 5
return @peaches_amount
end
def death
if age <60 return "I am dead"
else
end
end
end
當然可以。你的代碼有什麼問題? – usha
你會把它放在哪裏? –
我會在else語句中包含什麼? – user2759592