如何将可变数量的 args 传递给 yield。 我不想传递数组(如以下代码那样),实际上我想将它们作为参数的编程数量传递给 block 。
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
最佳答案
使用 splat 运算符 *
将数组转换为参数。
block.call(*array)
或
yield *array
关于ruby - 如何以编程方式将 args 传递给 Ruby 中的 yield?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/326825/