2

In Ruby, I have a question about capitals.

puts 'Answer "Yes"'
answer = gets.chomp
if answer == "Yes"
    puts "Great, you said Yes!!!"
else 
    puts "darn, how do I fix the capital issue?"
end

Here is my problem ... what if they write "yes" or "YES" it goes to the if/else statement.

Ilya
  • 13,337
  • 5
  • 37
  • 53

1 Answers1

3

Use String#downcase to include all variants of yes:

if answer.downcase == "yes"
  #do some 
else 
 ...
end

This solution works in cases "Yes", "YeS" and so on. If you want to check only "yes" or "YES" you can use include:

if %w(YES yes).include?(answer) # %w(YES yes) same as ["YES", "yes"]
  ...
end
Ilya
  • 13,337
  • 5
  • 37
  • 53