【发布时间】:2013-03-25 16:49:18
【问题描述】:
为 splat 参数设置默认值会出错:
1.9.3-p374 :001 > def a b, *c = nil
1.9.3-p374 :002?> end
SyntaxError: (irb):1: syntax error, unexpected '=', expecting ';' or '\n'
def a b, *c = nil
^
from /Users/me/.rvm/rubies/ruby-1.9.3-p374/bin/irb:16:in `<main>'
我尝试过的一些变体也不起作用:
1.9.3-p374 :003 > def a b, *c = []
1.9.3-p374 :005 > def a b, (*c) = nil
1.9.3-p374 :007 > def a b, (*c = [])
1.9.3-p374 :009 > def a b, (*c = [1,2,3])
1.9.3-p374 :011 > def a b, *c = [1,2,3]
我在这里没有看到不确定性问题,所以看起来应该是可能的。
相关:Why non-explicit splat param plus default param is wrong syntax for method definition in Ruby 1.9?
【问题讨论】:
-
Splat 参数默认为空数组
-
是的,但是默认参数呢?
-
您还没有接受答案有什么原因吗?
-
@vlasits 我希望有人能解决为什么 Ruby 不允许以通常的方式为 splats 设置默认参数的技术原因。我确实读过你所说的关于这是一个设计决策的内容。