学习Ruby并在教程中看到了这个例子
x = 10
5.times do |x|
puts "x inside the block: #{x}"
end
puts "x outside the block: #{x}"
输出以下内容
x inside the block: 0
x inside the block: 1
x inside the block: 2
x inside the block: 3
x inside the block: 4
x outside the block: 10
x如何在块内增加?这一定是一个非常简单的问题。
答案 0 :(得分:0)
5.times
迭代该块,然后从0到4进行5次取值。在循环中使用x是块的本地。执行循环后,x具有其原始值,即10.