TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Evils of the For Loop in Ruby

32 点作者 wolfish大约 16 年前

5 条评论

tptacek大约 16 年前
This is interesting, but kind of besides the point. What makes "for" evil in Ruby code is that it's non-idiomatic. A custom block "iterator" covers every case where you might use "for", and more elegantly. In 116,000 lines of Ruby code in our "toolshed" directory, I count <i>zero</i> uses of it; almost none of it is web code, and all our developers are ex-Pythonistas.
评论 #565773 未加载
评论 #565764 未加载
评论 #566973 未加载
mattmcknight大约 16 年前
It doesn't seem evil to me at all, it is exactly what I would expect.<p><pre><code> a = 1 for a in 1..2 b = a end p a &#62;&#62; 2 </code></pre> It seems natural that that would change the value of "a", I wouldn't expect a for loop to create a new scoping context. The example with putting a closure into the array is also what I would expect.<p>I am not sure what programming language has the scoping rules he expects, but for me there's nothing to see here, moving along...
评论 #565715 未加载
评论 #565733 未加载
lysium大约 16 年前
I don't get it. Isn't `|i|` the shorthand version of a lambda binding an i? And `lambda { i }` a lambda not binding anything? So why is the result surprising? I must be missing something.<p>Edit: I mean in<p><pre><code> each { |i| p i } </code></pre> the |i| stands for a shorthand lambda, that binds an i.<p>And in<p><pre><code> for i in 1..3 p i end </code></pre> there is no such binding. So where's the problem? That 'for' should use a lambda?
pkulak大约 16 年前
Isn't that what _all_ languages do with a for loop? Why should Ruby break the mold here?
omouse大约 16 年前
So syntax is to blame once again...