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
 146
Author: eebbesen, 2012-11-23

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)*' '
 194
Author: ,
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-07-31 12:02:14

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"
 244
Author: boulder_ruby,
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".

 34
Author: tint lwin lwin win,
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)
 18
Author: Bob Nadler,
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
 3
Author: Robert 'Jet' Rowe,
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(' ')
 3
Author: Muhamamd Awais,
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)

 0
Author: astee,
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