Ruby for in循环错误与{}块定界符

Ruby语言中,以下工作在irb中
for fruit in ['apple','banana','cherry','date'] do
    puts fruit 
end

但是这个没有

# error
for fruit in ['apple','date'] { puts fruit }

请注意参考以下块分隔符不要错误

5.times do |i|
    puts "hello "+i.to_s
end

5.times { |i| puts "hello "+i.to_s }

编辑:我想我正在观察的是与…的结果不一致的替代{}有人可以解释为什么或请指出我的错误?

解决方法

以下来自 this的帖子解释了“不一致”(强调我的):

end is a generic keyword that ends a context (conditionals,class and module definitions,method definitions,loops and exception-catching [statements]). Curly braces are special syntax for defining blocks or Hashes.

for循环是控制结构 – 而不是具有块参数的方法 – 因此它们必须用end关键字关闭.诸如Enumerable#times和Enumerable#的方法每个都有一个块参数,可以用{}或者do … end语法来定义.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...