首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > Ruby Rails >

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

2012-08-30 
Ruby 循环中的变量选择:局部变量还是实例变量From the ruby programming language.Blocks and Variable Sc

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

?

热点排行