红宝石的块范围 [英] Block scope in ruby

查看:72
本文介绍了红宝石的块范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,ruby块具有块范围,并且在块内部创建的所有变量将仅存在于该块之内.

My understanding was that ruby blocks have block scope, and all variables created inside block will live only within the block.

示例案例:

 food = ['toast', 'cheese', 'wine']
 food.each { |food| puts food.capitalize}
 puts food

输出:

"Toast"
"Cheese"
"Wine"
"Wine"

如果您将 food 变量放在块(每个块)中,则我的理解是它具有块范围.它仅存在于块范围内,对外部变量 food 没有任何影响.

If you take the food variable inside the block (Each block), my understanding was that it has block scope. It lives only within the block scope, and does not have any influence on the outer variable food.

但是行为不同,在这种情况下,修改了名为 food 的外部变量.这种理解是正确的吗,在ruby中,我们有块范围吗?

But the behavior is different, the outer variable named food is modified in this case. Is this understanding correct, In ruby do we have block scope?

推荐答案

这是ruby 1.8的预期行为.它已修复 在1.9版中.下面的代码片段使用ruby 1.9.3运行

This is expected behaviour for ruby 1.8. It was fixed in 1.9. Snippets below are run with ruby 1.9.3

food = ['toast', 'cheese', 'wine']
food.each { |food| puts food.capitalize.inspect} # !> shadowing outer local variable - food
puts food.inspect
# >> "Toast"
# >> "Cheese"
# >> "Wine"
# >> ["toast", "cheese", "wine"]

您是正确的,该块中的 food 仅限于该块,并以该名称遮盖了其他变量.但是,如果您对其执行破坏性操作,它将反映在原始数组中,因为它是对数组元素的引用,而不是其副本.观察:

You are correct, food from the block is scoped to that block and shadows other variables with this name. But if you do something destructive to it, it will be reflected in the original array, because it is reference to array element, not its copy. Observe:

food = ['toast', 'cheese', 'wine']

food.each { |f| f.capitalize} # transform and discard
food # => ["toast", "cheese", "wine"]

food.each { |f| f.capitalize! } # transform destructively (bang-version)
food # => ["Toast", "Cheese", "Wine"]

这篇关于红宝石的块范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆