How to have Ruby capitalize two words


#1

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

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.


#2

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.


#3

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

I already answered this question here: