hakuna matata
hakuna matata

Reputation: 3327

Return array elements to a variable length array in parameter

I have a ruby function with the following function header def get_value( default, *args ), and I'm using recursion in my function body, and I want to pass the args array without the first element, what I have used is get_value(default, args.slice(1, args.length)), however, after using a debugger, I found that the args in the recursed function was an array of arrays, and the inner arrays contained my elements. So if my main array was [:files, :mode] the recursed array would be [[:mode]]. How can I make it that is becomes [:mode]?

Upvotes: 0

Views: 64

Answers (1)

Matt
Matt

Reputation: 20786

Call get_value with the splat operator:

get_value(default, *args.slice(1, args.length))

or with additional brevity

get_value(default, *args[1..-1])

Upvotes: 3

Related Questions