7. Great work, problem with code


#1

Hello. Please help me

Testing code

print "What's your first name?"
first_name = gets.chomp.capitalize.capitalize!
puts "Your name is #{first_name}!"

print "What is your last name"
last_name = gets.chomp.capitalize.capitalize!
puts "Your last name is #{last_name}!"

print "What is your city"
city = gets.chomp.capitalize.capitalize!
puts "Your city is #{city}!"

print "What is your state in abbreviation"
state = gets.chomp.capitalize.upcase!
puts "Your state name is #{state}!"

What is wrong here?
It comes up with "!" when i answer the questions in the command box.


#2

Hi instead of diretly capitalize and upercase all your answer directly to the gets.comp try to do it like that

print "What's your first name?"
first_name = gets.chomp
first_name.capitalize.capitalize!
puts "Your name is #{first_name}!"

#3

Hey @tobias7400

The reason you're unable to print out the values of first_name, last_name and city is because they are assigned 'nil' instead of the capitalized string, and I'll go through the steps below as to how that occurs.

1: gets.chomp will return a string based on your input.

2: This new string then calls the capitalize method, capitalizing the first string value if necessary, and then return a new string.

3: After that, the return string from the normal capitalize method, then calls the capitalize! method. This is the version that modifies the original string, but will only return a new string if there is anything to capitalize, and otherwise return 'nil'.
Since the string has already been capitalized, the last method call will always return 'nil', and this is assigned to the name & city variables.

Note, that since the state variable runs .upcase! instead of .capitalize, it will assign the variable with the upcase string unless the input string is already in upper case.

I've attached an example below that might demonstrate the same a bit more clearly.

my_string = gets.chomp
#I entered "my string" as input

puts my_string.capitalize
#output = "My string"
# my_string.capitalize will return a new string with first letter capitalized

puts my_string
#output = "my string"
# Since the previous method call didn't modify the variable, the first letter here is still lower case

puts my_string.capitalize.capitalize!
#output = ""
# Here's the same issue as you've had. my_string.capitalize returns "My string", which will return 'nil' since "My string".capitalize! doesn't need to do anything

puts my_string
#output = "my string"
# Since the capitalize! call happened on the return value of my_string.capitalize, the original string is still not modified

puts my_string.capitalize!
#output = "My string"
# Here we run the self-modifying capitalize! on the string variable, and with it having first letter as lower-case, it will modify the actual string value as well as return a string with the new value

puts my_string
#output = "My string"
# Here we can see the value has been modified

puts my_string.capitalize!
#output = ""
# Since my_string is now "My string", calling capitalize! will return 'nil' since nothing has been changed