我有一種方法,一次只能加載50
對象。紅寶石分頁加載資源
所以我拿出這塊Ruby代碼保持加載,直到有沒有更多的結果:
objects = []
offset = 0
limit = 50
loop do
# Load paged objects using the current given offset
new_objects = load_objects(some_url, limit: limit, start: offset)
offset += limit
objects.concat(new_objects)
# End the loop as soon as no more results are returned
break if new_objects.count == 0
end
現在,雖然這偉大工程,我想知道如果有一個更簡潔的方式來在ruby中執行此任務。
更新:我想到的一些collect
樣的做法是這樣的:
# Pseudocode
objects = update_while_true([],0) do |result_array, limit|
new_objects = load_objects(some_url, limit: 50, start: current)
result_array.concat(new_objects)
limit += 50
# Should the loop be run again?
new_objects.count > 0
end
解釋這些變量/方法。 – sawa