Ruby escribe en mayúscula cada palabra primera letra
Necesito hacer el primer carácter de cada palabra mayúscula, y hacer el resto minúscula...
manufacturer.MFA_BRAND.first.upcase
Es solo poner la primera letra en mayúscula, pero necesito esto:
ALFA ROMEO => Alfa Romeo
AUDI => Audi
BMW => Bmw
ONETWO THREE FOUR => Onetwo Three Four
7 answers
Prueba esto:
puts 'one TWO three foUR'.split.map(&:capitalize).join(' ')
#=> One Two Three Four
O
puts 'one TWO three foUR'.split.map(&:capitalize)*' '
En Rails:
"kirk douglas".titleize => "Kirk Douglas"
#this also works for 'kirk_douglas'
Sin Rieles:
"kirk douglas".split(/ |\_/).map(&:capitalize).join(" ")
#OBJECT IT OUT
def titleize(str)
str.split(/ |\_/).map(&:capitalize).join(" ")
end
Sin Rails (cargar el soporte activo de rails para patch #método titleize a String
)
require 'active_support/core_ext'
"kirk douglas".titleize #=> "Kirk Douglas"
(algunos) casos de uso de cadenas manejados por # titleize
- "kirk douglas"
- "kirk_douglas"
- "kirk-douglas"
- "kirkDouglas"
- "KirkDouglas"
#titleize gotchas
El método # titleize es un poco más complejo de lo que uno podría esperar inicialmente y puede producir inesperados resultados, especialmente con situaciones sensibles a mayúsculas y minúsculas como señaló @JamesMcMahon:
"hEy lOok".titleize #=> "H Ey Lo Ok"
Porque está destinado a manejar código de carcasa de camello como:
"kirkDouglas".titleize #=> "Kirk Douglas"
Para tratar este caso de borde, podría limpiar su cadena con #downcase
primero antes de ejecutar #titleize. Por supuesto, si lo hace, eliminará cualquier separación de palabras basada en camellos:
"kirkDouglas".downcase.titleize #=> "Kirkdouglas"
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
2014-06-17 22:54:38
"hello world".titleize
que debería dar como resultado "Hola Mundo".
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
2015-05-11 06:37:29
Otra opción es usar una expresión regular y gsub, que toma un bloque:
'one TWO three foUR'.gsub(/\w+/, &:capitalize)
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
2013-12-16 12:40:06
Busque en el método String#capitalize.
Http://www.ruby-doc.org/core-1.9.3/String.html#method-i-capitalize
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
2012-11-22 21:25:50
"hello world".split.each{|i| i.capitalize!}.join(' ')
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
2012-11-23 03:45:34
Si está tratando de poner en mayúscula la primera letra de cada palabra en una matriz, simplemente puede poner esto:
Array_name.map ( & : capitalizar)
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
2017-11-19 10:57:13