Ruby tiene una cadena.startswith ("abc") construido en el método?
¿Ruby tiene un método some_string.starts_with("abc")
integrado?
4 answers
Se llama String#start_with?
, not String#startswith
: En Ruby, los nombres de los métodos booleanos terminan con ?
y las palabras en los nombres de los métodos se separan con _
. No estoy seguro de dónde fue el s
, personalmente, preferiría String#starts_with?
sobre el String#start_with?
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-11-09 04:48:39
El título de la pregunta y el cuerpo de la pregunta son diferentes. Ruby no tiene un starts_with? método. Rails, que es un framework Ruby , sin embargo, lo hace, como dice sepp2k. Ver su comentario en su respuesta para el enlace a la documentación para ello.
Siempre puedes usar una expresión regular:
if SomeString.match(/^abc/)
# SomeString starts with abc
^
significa "inicio de cadena" en expresiones regulares
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-11-09 04:12:04
Si esto es para un proyecto no Rails, usaría String#index
:
"foobar".index("foo") == 0 # => true
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-11-09 09:23:12
Puede usar String =~ Regex
. Devuelve la posición de la coincidencia regex completa en la cadena.
irb> ("abc" =~ %r"abc") == 0
=> true
irb> ("aabc" =~ %r"abc") == 0
=> false
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-11-09 04:00:04