开发者

pass array into vararg in ruby?

开发者 https://www.devze.com 2023-02-15 05:07 出处:网络
The ruby exec() function takes a vararg for its second parameter to provide arg开发者_开发知识库uments to the program being executed. However, I would like to pass an array of arguments (for various r

The ruby exec() function takes a vararg for its second parameter to provide arg开发者_开发知识库uments to the program being executed. However, I would like to pass an array of arguments (for various reasons). I could work around this by just giving exec a completed string but that involves the shell (and escaping possible parameters). Additionally, as far as I can tell, collapsing the arguments into one string will pass them as one argument to my program -- I want their distinctness to be preserved. Is it possible to pass an array to a varargs argument in a ruby function? (note that, in this case, I can't modify exec() to accept any wrapping or shifts).


You can use the splat operator like this:

exec("echo", *["hello","world"])
0

精彩评论

暂无评论...
验证码 换一张
取 消