Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有一个数组。我希望将数组传递给函数。然而,该函数需要两个参数。有没有办法即时将数组转换为 2 个参数?例如:
a = [0,1,2,3,4] b = [2,3] a.slice(b)
会在 Ruby 中产生错误。我需要输入a.slice(b[0],b[1])我正在寻找更优雅的东西,如a.slice(foo.bar(b)) 谢谢。
a.slice(b[0],b[1])
a.slice(foo.bar(b))
您可以使用(或“splat”)运算符将 anArray转换为参数列表:*
Array
*
a = [0, 1, 2, 3, 4] # => [0, 1, 2, 3, 4] b = [2, 3] # => [2, 3] a.slice(*b) # => [2, 3, 4]
用这个
a.slice(*b)
它被称为 splat 运算符