专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Ruby/Rails

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

发布时间:2011-06-29 18:30:32 文章来源:www.iduyao.cn 采编人员:星星草
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

 

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: