How to have Ruby capitalize two words


print "What city are you from?"
city = gets.chomp

Using this code, if I were to enter fort wayne as my answer, the code will return Fort wayne. How could I get it to capitalize both words.


the .capitalize is just to make the sentence look better,

you could do city.downcase!.capitalize! to have a good layout for your sentence but for your question you'd have to rely on the User putting in the correct words with capitalization where it needs to be. And then take out the .capitalize! (and whatever else is attached to the city variable.)

This is why you see Programmers in video games having the States the user comes from, already in a predetermined field so there's no way to mess up the State, and the City (If they an option for it). Is left for the User to input correctly, or to mess up to their whim.


You need to split the string, capitalize all words and then join the string back.

I already answered this question here: