如何以编程方式将args传递给Ruby?

Ben*_*use 11 ruby

如何将可变数量的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)