Advanced Learning 1/7


#1

The default code prompts you to enter your first name, last name, city, and state.
It capitalizes the first letter of your first and last name, the first letter of your city, and all letters of your state.

What if the user resides in a city with more than one word (e.g. Los Angeles or New York)? The program won't capitalize the first letter of the second word. Additionally what if the user, when prompted for a state, enters the full name and not the abbreviation? The program is set to capitalize all letters so you will end up with CALIFORNIA or NEW YORK.

What code can be entered to address these issues? The answer is not needed for the exercise but instead for my own knowledge.


How to have Ruby capitalize two words
#2

To capitalize all letters from all words, you would need to break down the string into an array of words, capitalize all words and then join the array back into a string.

Something like this:

city = "new york".split.collect(&:capitalize).join(" ")

For the state being a full name, you would need to count the letters and have an if/else statement to handle both cases:

if state.length <= 2
state.upcase!
else
state.capitalize!
end