Ruby capitalize every word first letter

RubyString

Ruby Problem Overview


I need to make the first character of every word uppercase, and make the rest lowercase...

manufacturer.MFA_BRAND.first.upcase

is only setting the first letter uppercase, but I need this:

ALFA ROMEO => Alfa Romeo
AUDI => Audi
BMW => Bmw
ONETWO THREE FOUR => Onetwo Three Four

Ruby Solutions


Solution 1 - Ruby

In Rails:

"kirk douglas".titleize => "Kirk Douglas"
#this also works for 'kirk_douglas'

w/o Rails:

"kirk douglas".split(/ |\_/).map(&:capitalize).join(" ")

#OBJECT IT OUT
def titleize(str)
  str.split(/ |\_/).map(&:capitalize).join(" ")
end

#OR MONKEY PATCH IT
class String  
  def titleize
    self.split(/ |\_/).map(&:capitalize).join(" ")
  end
end

w/o Rails (load rails's ActiveSupport to patch #titleize method to String)

require 'active_support/core_ext'
"kirk douglas".titleize #=> "Kirk Douglas"

(some) string use cases handled by #titleize

  • "kirk douglas"
  • "kirk_douglas"
  • "kirk-douglas"
  • "kirkDouglas"
  • "KirkDouglas"

#titleize gotchas

Rails's titleize will convert things like dashes and underscores into spaces and can produce other unexpected results, especially with case-sensitive situations as pointed out by @JamesMcMahon:

"hEy lOok".titleize #=> "H Ey Lo Ok"

because it is meant to handle camel-cased code like:

"kirkDouglas".titleize #=> "Kirk Douglas"

To deal with this edge case you could clean your string with #downcase first before running #titleize. Of course if you do that you will wipe out any camelCased word separations:

"kirkDouglas".downcase.titleize #=> "Kirkdouglas"

Solution 2 - Ruby

try this:

puts 'one TWO three foUR'.split.map(&:capitalize).join(' ')

#=> One Two Three Four

or

puts 'one TWO three foUR'.split.map(&:capitalize)*' '

Solution 3 - Ruby

"hello world".titleize which should output "Hello World".

Solution 4 - Ruby

Another option is to use a regex and gsub, which takes a block:

'one TWO three foUR'.gsub(/\w+/, &:capitalize)

Solution 5 - Ruby

"hello world".split.each{|i| i.capitalize!}.join(' ')

Solution 6 - Ruby

Solution 7 - Ruby

If you are trying to capitalize the first letter of each word in an array you can simply put this:

array_name.map(&:capitalize)

Solution 8 - Ruby

I used this for a similar problem:

'catherine mc-nulty joséphina'.capitalize.gsub(/(\s+\w)/) { |stuff| stuff.upcase }

This handles the following weird cases I saw trying the previous answers:

  • non-word characters like -
  • accented characters common in names like é
  • capital characters in the middle of the string

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbyCoderView Question on Stackoverflow
Solution 1 - Rubyboulder_rubyView Answer on Stackoverflow
Solution 2 - Rubyuser904990View Answer on Stackoverflow
Solution 3 - Rubytint lwin lwin winView Answer on Stackoverflow
Solution 4 - RubyBob NadlerView Answer on Stackoverflow
Solution 5 - RubyMuhamamd AwaisView Answer on Stackoverflow
Solution 6 - RubyRobert 'Jet' RoweView Answer on Stackoverflow
Solution 7 - RubyasteeView Answer on Stackoverflow
Solution 8 - Rubysesquipedalian-devView Answer on Stackoverflow