我有一个哈希数组,如下所示:
items = [ {"id" => 1, "cost" => '2.00'},
{"id" => 2, "cost" => '6.00'},
{"id" => 1, "cost" => '2.00'},
{"id" => 1, "cost" => '2.00'},
{"id" => 1, "cost" => '2.00'} ]
我想更新cost to '8.00'
. id = 1
我已经尝试过each
类似下面的方法,但我想知道是否有另一种更有效的更新值的方法?
items.each { |h| h["cost"] = "8.00" if h["id"] == 1 }