如何将可变数量的args传递给yield.我不想传递一个数组(如下面的代码那样),我实际上想将它们作为程序化数量的args传递给块.
def each_with_attributes(attributes, &block)
results[:matches].each_with_index do |match, index|
yield self[index], attributes.collect { |attribute| (match[:attributes][attribute] || match[:attributes]["@#{attribute}"]) }
end
end
Run Code Online (Sandbox Code Playgroud)
And*_*isi 14
使用splat-operator *将数组转换为参数.
block.call(*array)
Run Code Online (Sandbox Code Playgroud)
要么
yield *array
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3988 次 |
| 最近记录: |