使用数组将多个参数传递给变量参数 ruby 方法

Passing multiple arguments to variable argument ruby method using array

我现在正在使用 ruby gem,它接受可变数量的参数(具体来说,它是 axlsx gem)。

我正在使用 column_widths 函数,定义为:

def column_widths(*widths)
  widths.each_with_index do |value, index|
    next if value == nil
    Axlsx::validate_unsigned_numeric(value) unless value == nil
    find_or_create_column_info(index).width = value
  end
end

我有一个动态数量的宽度需要设置(因为列数不同),所以我尝试创建一个宽度数组并将其传入,但它将数组视为单个参数。

如何将数组作为参数列表传递?

编辑:

实际错误为:

Invalid Data [30, 13, 20, 13, 20, 13, 20, 13, 10, 10, 10, 13, 20, 10] for Invalid column width. must be [Fixnum, Integer, Float]

您可以在方法定义和方法调用中使用 splat。这应该有效:

column_widths(*array_of_widths)