Pregunta

Ruby's documentation displays method signatures as:

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

which looks like an array to me, but it is not. You can pass a single string or various string as arguments, like so:

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

How do I pass an array as a list of arguments? The following does not work:

"hello".start_with?(["heaven", "hell"])
¿Fue útil?

Solución

The brackets are a documentation convention for optional so the brackets in

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

are just saying that you can call start_with? with zero or more prefixes. This is a common convention in documentation, you'll see it the jQuery documentation, Backbone documentation, MDN JavaScript documentation, and pretty much any other software documentation.

If you have an array of prefixes that you want to use with start_with?, then you can splat the array to unarrayify it thusly:

a = %w[heaven hell]
'hello'.start_with?(*a)           # true
a = %w[where is]
'pancakes house?'.start_with?(*a) # false
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top