看看这段有点傻的代码。
3.times do |round|
cell = uninitialized Int32
if cell == 60
puts "cell = 60, round = #{round}"
puts " (by chance!)" if round == 0
end
cell = 60
end
是每次迭代都重新创建 cell,碰巧放在上一次迭代中 cell 使用的地址上,所以第三次经过时值才是 60?
还是同一个变量一直存在于全部三次迭代中,因此我可以依赖这种行为?
从英语翻译·查看原文Consider this rather silly snippet:
3.times do |round|
cell = uninitialized Int32
if cell == 60
puts "cell = 60, round = #{round}"
puts " (by chance!)" if round == 0
end
cell = 60
end
Does every iteration create cell again, coincidentally placing it at the address used by cell on the preceding iteration (which would explain its value of 60 on the third pass)?
Or does one variable persist across all three iterations, so that I can rely on that?