2

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...

Nat
  • 2,689
  • 2
  • 29
  • 35

2 Answers2

5

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?

Community
  • 1
  • 1
knut
  • 27,320
  • 6
  • 84
  • 112
1

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

Michael Kohl
  • 66,324
  • 14
  • 138
  • 158