Ruby for 循环陷阱? [英] Ruby for loop a trap?

查看:52
本文介绍了Ruby for 循环陷阱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在讨论 Ruby 循环时,Niklas B. 最近谈到了 for 循环不引入新的作用域"',与每个循环相比.我想看一些例子来说明人们对此有何感受.

好的,我扩展了这个问题:在 Ruby 的其他地方,我们可以看到 apears 做什么/结束块分隔符,但实际上里面没有范围?除了为...做...结束?

好的,问题的另一个扩展,有没有办法用花括号 { block } 编写 for 循环?

解决方案

让我们通过一个例子来说明这一点:

结果 = [](1..3).each do |i|结果<<拉姆达{我}结尾p results.map(&:call) # =>[1,2,3]

很酷,这正是预期的结果.现在检查以下内容:

结果 = []因为我在 1..3结果<<拉姆达{我}结尾p results.map(&:call) # =>[3,3,3]

咦,怎么回事?相信我,这类错误很难追踪.Python 或 JS 开发人员会明白我的意思:)

仅凭这一点,我就可以避免像瘟疫一样出现这些循环,尽管有更多好的论据支持这一立场.正如 Ben 正确指出的那样,使用 Enumerable 中的正确方法几乎总能比使用普通代码产生更好的代码旧的命令式 for 循环或更高级的 Enumerable#each.例如,上面的例子也可以简明地写成

lambdas = 1.upto(3).map { |i|拉姆达{我}}p lambdas.map(&:call)

<块引用>

我扩展了这个问题:在 Ruby 的其他地方,我们可以看到 apears 做什么/结束块分隔符,但实际上里面没有作用域?除了为...做...结束?

每个循环结构都可以这样使用:

while true do#...结尾直到假做# ...结尾

另一方面,我们可以在没有 do 的情况下编写所有这些(显然更可取):

for i in 1..3结尾虽然是真的结尾直到假结尾

<块引用>

问题的另一个扩展,有没有办法用花括号{块}编写for循环

不,没有.另请注意,术语块"在 Ruby 中具有特殊含义.

In a discussion of Ruby loops, Niklas B. recently talked about for loop 'not introducing a new scope', as compared to each loop. I'd like to see some examples of how does one feel this.

O.K., I expand the question: Where else in Ruby do we see what apears do/end block delimiters, but there is actually no scope inside? Anything else apart from for ... do ... end?

O.K., One more expansion of the question, is there a way to write for loop with curly braces { block } ?

解决方案

Let's illustrate the point by an example:

results = []
(1..3).each do |i|
  results << lambda { i }
end
p results.map(&:call)  # => [1,2,3]

Cool, this is what was expected. Now check the following:

results = []
for i in 1..3
  results << lambda { i }
end
p results.map(&:call)  # => [3,3,3]

Huh, what's going on? Believe me, these kinds of bugs are nasty to track down. Python or JS developers will know what I mean :)

That alone is a reason for me to avoid these loops like the plague, although there are more good arguments in favor of this position. As Ben pointed out correctly, using the proper method from Enumerable almost always leads to better code than using plain old, imperative for loops or the fancier Enumerable#each. For instance, the above example could also be concisely written as

lambdas = 1.upto(3).map { |i| lambda { i } }
p lambdas.map(&:call)

I expand the question: Where else in Ruby do we see what apears do/end block delimiters, but there is actually no scope inside? Anything else apart from for ... do ... end?

Every single one of the looping constructs can be used that way:

while true do
  #...
end

until false do
  # ...
end

On the other hand, we can write every one of these without the do (which is obviously preferrable):

for i in 1..3
end

while true
end

until false
end

One more expansion of the question, is there a way to write for loop with curly braces { block }

No, there is not. Also note that the term "block" has a special meaning in Ruby.

这篇关于Ruby for 循环陷阱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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