mosquito242 mosquito242 - 11 months ago 54
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

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

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?


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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download