I am using the following code to compare strings but it always takes me to the else. Why?
print("Enter your state abbreviation: ")
state_abbreviation = gets
if state_abbreviation.upcase == "NC"
puts("North Carolina")
elsif state_abbreviation.upcase == "SC"
puts("Sourth Carolina")
elsif state_abbreviation.upcase == "GA"
puts("Georgia")
elsif state_abbreviation.upcase == "FL"
puts("Florida")
elsif state_abbreviation.upcase == "AL"
puts("Alabama")
else
puts("You have enter wrong abbreviation")
end
I also have tried .eql?("string") but I get the same result.
The string returned by gets will have a linebreak at the end. Use
String#chompto remove it (i.e.state_abbreviation = gets.chomp).PS: Your code would look much cleaner (IMHO) if you used case-when instead of if-elsif-elsif.