3

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_widths 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]

3
  • 1
    I think you're doing ok, what is the actual error? Btw, this question looks identical. Commented Sep 24, 2015 at 5:30
  • edited to reflect the actual error, and I looked at that, but it didn't answer my question or solve my problem Commented Sep 24, 2015 at 7:32
  • nevermind @Lucio, you're right - I saw that but somehow I skipped over the answers other than the top one. my bad. Commented Sep 24, 2015 at 7:37

1 Answer 1

7

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

column_widths(*array_of_widths)
Sign up to request clarification or add additional context in comments.

1 Comment

that worked - thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.