2

我正在编写一个方法来包装另一个方法作为类的一部分。它需要接受和传递任意数量的参数。

就像是...

def do(*things)
  old_method(things)
end

除了这不起作用,因为 old_method 需要将 things 数组的内容作为单独的参数而不是作为数组传递。

我只是想不出在 ruby​​ 语法中做到这一点的方法......

4

2 回答 2

5

您可以使用:

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

那是一个 splat 运算符,请参阅例如What does the (unary) * operator do in this Ruby code?或者 splat 在这里做什么?

于 2012-04-06T22:32:45.403 回答
1

同样的方式你得到的论点,old_method(*things)

于 2012-04-06T22:32:29.163 回答