Ruby块中奇怪的imoperfection
可能重复:
Ruby中这些块编码样式的区别或价值是什么?
# This works method :argument do other_method end # This does not method :argument { other_method }
为什么?
似乎解释器很困惑并且认为{…}是哈希。
当解释器无法理解实际有效的代码时,我总是生气。 它类似于PHP,有很多这类问题。
它不认为它是哈希 – 它是一个优先问题。 {}
绑定比do end
更严格,所以method :argument { other_method }
被解析为method(:argument {other_method})
,这在语法上是无效的(但如果不是符号,则参数将是另一个方法调用) 。
如果你添加括号( method(:argument) { other_method }
),它将正常工作。
不,代码实际上并不有效。 如果是的话,它会起作用。