解决方法
星号*表示将所有剩余的参数组合到由参数命名的单个列表中. &符号&意味着如果给方法调用一个块(即block_given?将为true),则将其存储在由参数命名的新Proc中(或者我猜是伪参数).
def foo(*a) puts a.inspect end foo(:ok) # => [:ok] foo(1,2,3) # => [1,3] def bar(&b) puts b.inspect end bar() # => nil bar() {|x| x+1} # => #<Proc:0x0000000100352748>
注意&必须出现在最后,如果使用,并且*可能在它之前是倒数第二个,或者它必须是最后一个.
*运算符也可用于在调用时将数组“扩展”为参数列表(而不是在定义中“组合”它们),如下所示:
def gah(a,b,c) puts "OK: a=#{a},b=#{b},c=#{c}" end gah(*[1,3]) # => "OK: a=1,b=2,c=3" gah(1,*[2,c=3" # must be last arg.
同样,&运算符可用于在调用函数时将Proc对象“展开”为给定块:
def zap yield [1,3] if block_given? end zap() # => nil zap(&Proc.new{|x|puts x.inspect}) # => [1,3]