def find_users_online(count = 1)
users = Array.new
count.times do
users += get_users_online
end
users # <==== I want to remove this here
end
在上面的代碼中,im必須在函數的末尾再次放置「users」變量以返回正確的值(用戶)。但是有可能時間塊返回用戶值,我可以刪除功能結束時的「用戶」?有沒有更清晰的方式來寫這個? (Ruby/Rails塊,返回值)
def find_users_online(count = 1)
users = Array.new
count.times.and_return do # <== something like this
users += get_users_online
end
end
因爲在代碼結束之前讓你的「返回指示」有四行可讀性或直觀性更強嗎?想象一下,如果你的街區長達100行,你會如何看懂它? – 2010-10-24 16:01:09
你爲什麼要多次調用'get_users_online'? – 2010-10-24 16:14:27
@Platinum:如果他的區塊長度爲100行,他會遇到比這個更大的問題...... ;-) – 2010-10-24 16:17:38