Ruby: String Comparison Issues
Solution 1:
gets
returns the entire string entered, including the newline, so when they type "M" and press enter the string you get back is "M\n"
. To get rid of the trailing newline, use String#chomp
, i.e replace your first line with answer = gets.chomp
.
Solution 2:
The issue is that Ruby is including the carriage return in the value.
Change your first line to:
answer = gets().strip
And your script will run as expected.
Also, you should use puts
instead of two print
statements as puts
auto adds the newline character.