【发布时间】:2016-05-19 19:58:32
【问题描述】:
我有一个散列数组,我不想修改每个散列。所以我正在迭代我的源数据——在这个例子中只是迭代数字并修改每个哈希值。 但是在迭代器的上下文之外,只修改了数组的一个元素而不是所有元素,并且数组的第一个元素被最后一个元素覆盖。
arr = [{ id: 1 }, { id: 2 }, { id: 3 }]
1.upto(3) do |i|
a = arr.detect { |t| t[:id] = i }
a[:content] = 'this is my content'
end
puts arr
输出
{:id=>3, :content=>"this is my content"}
{:id=>2}
{:id=>3}
预期输出
{:id=>1, :content=>"this is my content"}
{:id=>2, :content=>"this is my content"}
{:id=>3, :content=>"this is my content"}
【问题讨论】:
-
Pascal Turbo 不能与 Turbo Pascal 混淆。