!capitalize returns blanks in string



Is this code correct? When I run it the variables return blanks.

print "What's your first name?"
first_name = gets.chomp.capitalize!

print "What's your last name?"
last_name = gets.chomp.capitalize!

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

print "What state or province are you from?"
state = gets.chomp.upcase!

print "Your name is #{first_name} #{last_name} and you are from #{city}, #{state}. "


Since your program takes input, it seems relevant to your question what input you are providing.


This code seems to be reacting relatively weird... I just tried copying/pasting your code. When I entered something capitalized for first name, last name, city, and state - it printed out blanks for the first three, and then printed out the state. When I entered all of the above in lower case, it worked perfectly fine. The way that seems to always (pretty sure) word would be to use 1 line after each of them to perform the capitalization/upcase...

first_name = gets.chomp


Well done, you've found some input that reproduces the issue.

Next up is finding out what values the "blank" variables refer to, and where they are coming from.

Documentation for the String method capitalize!