红宝石转换数组函数参数 [英] ruby convert array into function arguments

查看:104
本文介绍了红宝石转换数组函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个数组。我希望将数组传递给函数。的功能,但是,有两个参数。有没有动态数组转换成2个参数的方法吗?
例如:

Say I have an array. I wish to pass the array to a function. The function, however, expects two arguments. Is there a way to on the fly convert the array into 2 arguments? For example:

a = [0,1,2,3,4]
b = [2,3]
a.slice(b)

在会产生Ruby的一个错误。我需要输入 a.slice(B [0],B [1])我要寻找的东西更优雅,如 a.slice (foo.bar(b))
谢谢你。

Would yield an error in Ruby. I need to input a.slice(b[0],b[1]) I am looking for something more elegant, as in a.slice(foo.bar(b)) Thanks.

推荐答案

使用此

a.slice(*b)

这就是所谓的图示运营商

It's called the splat operator

这篇关于红宝石转换数组函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆