mosquito242 mosquito242 - 4 days ago 4
Ruby Question

Passing multiple arguments to variable argument ruby method using array

I'm working with a ruby gem right now that takes in a variable number of arguments (to be specific, it's the axlsx gem).

I'm using the

column_width
s function, defined as:

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


I have a dynamic number of widths that need to be set (because the number of columns varies), so I tried creating an array of widths and passing that in, but it treats the array as a single argument.

How can I pass in the array as a list of arguments?

Edit:

The actual error is:


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]

Answer

You can use the splat both at method definition and method invocation. This should work:

column_widths(*array_of_widths)