鲁比的 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
我是一名优秀的程序员,十分优秀!