gpt4 book ai didi

ruby - 如何将数组作为参数列表传递

转载 作者:太空宇宙 更新时间:2023-11-03 17:16:46 25 4
gpt4 key购买 nike

鲁比的 documentation将方法签名显示为:

start_with?([prefixes]+) → true or false

这对我来说看起来像一个数组,但它不是。您可以将单个字符串或多个字符串作为参数传递,如下所示:

"hello".start_with?("heaven", "hell")     #=> true

如何将数组作为参数列表传递?以下不起作用:

"hello".start_with?(["heaven", "hell"])

最佳答案

括号是可选的的文档约定,因此

中的括号

start_with?([prefixes]+) → true or false

只是说您可以使用零个或多个前缀调用start_with?。这是文档中的常见约定,您会在 jQuery 中看到它文档,Backbone文档,MDN JavaScript文档,以及几乎所有其他软件文档。

如果你有一个前缀数组,你想使用 start_with?,那么你可以 splat 数组来取消数组化:

a = %w[heaven hell]
'hello'.start_with?(*a) # true
a = %w[where is]
'pancakes house?'.start_with?(*a) # false

关于ruby - 如何将数组作为参数列表传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16258975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com