是否可以将数组转换为函数的参数列表?

Is it possible to turn an array into a list of parameters for a function?

我有一个接受 n 个参数的函数(它是 devinus/sh 函数),您可以在其中执行命令行程序并获得结果:

示例:Sh.file "-b", "--mime-type", path_to_file

但我想将参数放在一个数组中,这样它们就可以根据函数的调用方式而变化。

我想要的示例:

data = ["-b", "--mime-type", path_to_file]

# a way of going through the array and turning it into the parameters for the Sh function

Sh.file <loop array params here>

Sh 不接受数组作为参数。

有什么想法吗?

您可以使用 apply(Sh, :file, args),其中 args 是参数数组。