Nat
Nat

Reputation: 3077

passing an unknown number of Arguments to a ruby function

I'm writing a method to wrap another method as part of a class. It needs to both take and pass on an arbitrary number of arguments.

Something like...

def do(*things)
  old_method(things)
end

except this won't work because old_method needs to be passed the contents of the things array as separate arguments rather than as an array.

I just can't think of a way to do this within ruby syntax...

Upvotes: 3

Views: 3865

Answers (2)

knut
knut

Reputation: 27845

You can use:

def do(*things)
  old_method(*things)
end

That's a splat operator, see e.g. What does the (unary) * operator do in this Ruby code? or What's the splat doing here?

Upvotes: 6

Michael Kohl
Michael Kohl

Reputation: 66837

Same way you get the arguments, old_method(*things).

Upvotes: 1

Related Questions