我正在使用以下邏輯根據條件更新列表項目。什麼是根據匹配條件替換數組元素的優雅方法?
def update_orders_list(order)
@orders.delete_if{|o| o.id == order.id}
@orders << order
end
理想情況下,我寧願這些方法:
array.find_and_replace(obj) { |o| conditon }
OR
idx = array.find_index_of { |o| condition }
array[idx] = obj
有沒有更好的辦法?
我在Array文檔中找不到這個,因爲我在查看1.8.6文檔。謝謝。 – 2010-09-15 20:17:51