为什么 `a (bc do;end)` 在语法上不是正确的 Ruby 程序?
为什么在 ruby18 和 ruby19 中给这段代码一个语法错误:
a (b.c do;end)
我本以为它的含义如下。使用一个参数调用 a
方法。空格后面的括号不是方法参数括号,而只是普通的括号,就像您几乎可以放在任何地方一样。参数是对带有块的对象 b
调用方法 c
的返回值。
然而,以下所有内容都被 ruby18 解释为语法上正确的。 ruby19 仅将这些示例中的第一个视为语法不正确。
a (b do;end)
和:
a (b.c {})
和:
(b.c do;end)
Why gives this code in ruby18 and ruby19 a syntax error:
a (b.c do;end)
I would have expected it to mean the following. A call to the method a
with one argument. The parentheses after the space are not method argument parentheses, but only normal parentheses like you can put almost everywhere. The argument is the return value of the call to the method c
on the object b
with a block.
All of the following are however interpreted as syntactically correct by ruby18. Only the first of these examples is treated as syntactically incorrect by ruby19.
a (b do;end)
and:
a (b.c {})
and:
(b.c do;end)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除方法名称和
(
之间的空格。Ruby 曾经警告说这是不可靠的,在某些情况下它似乎确实让 ruby 1.9 无法使用。Remove the space in between the method name and the
(
. Ruby used to warn that this was dodgy and it does seem to throw ruby 1.9 off in some cases.