这段代码:
def func *; end
[func "hello"]
在 Ruby 1.8.7 中解析没有错误,但返回语法错误:
syntax error, unexpected ']', expecting '}'
在 Ruby 中 >= 1.9。我浏览了 What is the difference between Ruby 1.8 and Ruby 1.9 ,但找不到对此的引用。有谁知道是什么变化导致了这种情况?
最佳答案
这是为了避免歧义。请考虑以下事项:
def foo(a, b = 1) # foo takes an optional second argument
end
[foo 1, 2]
这可以解释为 [(foo 1), 2]
或 [(foo 1, 2)]
。
Calling Methods 中有两个引用文档(虽然与数组文字没有直接关系):
Note that the parenthesis are optional ... Except when there is difference between using and omitting parentheses
In many cases parenthesis are not necessary when sending a message ... However, parenthesis are necessary to avoid ambiguity.
关于ruby - 为什么这段代码适用于 ruby 1.8 而不是 ruby 1.9?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18719672/