ruby - 为什么这段代码适用于 ruby​​ 1.8 而不是 ruby​​ 1.9?

标签 ruby compatibility ruby-1.9 ruby-1.8

这段代码:

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/

相关文章:

ruby - 为什么卡住哈希文字与卡住字符串文字不同?

ruby - 如何阻止上帝留下陈旧的 Resque 工作流程?

ruby-on-rails - Ruby on Rails - Postgres : Array value must start with "{" or dimension information

sql-server - SQL Server 数据库的兼容级别

javascript - Firefox 插件与版本 11 的兼容性

从 Xcode shell 脚本运行时缺少 Ruby gems

ruby-on-rails - 为什么Ruby 1.9.2带有JSON gem依赖关系而爆炸?

ruby - Ruby 中的 Object 和 BasicObject 有什么区别?

ruby - 如何在 Ruby 中取消转义 JavaScript 字符串

Windows 7 x64 上的 Delphi 问题?