读书人

Ruby 循环中的变量取舍:局部变量还是

发布时间: 2012-08-30 09:55:54 作者: rapoo

Ruby 循环中的变量选择:局部变量还是实例变量

From the ruby programming language.

Blocks and Variable Scope

Blocks define a new variable scope: variables created within a block exist only within that block and are undefined outside of the block.

?

Be cautious, however; the local variables in a method are available to any blocks within that method.

?

So if a block assigns a value to a variable that is already defined outside of the block, this does not create a new block-local variable but instead assigns a new value to the already-existing variable. Sometimes, this is exactly the behavior we want.

Referance
http://stackoverflow.com/questions/1654637/ruby-forgets-local-variables-during-a-while-loop

?

读书人网 >Ruby Rails

热点推荐