这是否将Ruby块传递给自身?

时间:2012-09-01 18:52:15

标签: ruby

学习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如何在块内增加?这一定是一个非常简单的问题。

1 个答案:

答案 0 :(得分:0)

5.times迭代该块,然后从0到4进行5次取值。在循环中使用x是块的本地。执行循环后,x具有其原始值,即10.