17. Case of many opinions.. How to ignore capital letters?



My code works fine, but how do I ignore uppercase/lowercase letters entered by the user? I added .capitalize for "Ruby" & "Python", but that wouldn't be ideal for JavaScript, CSS, HTML.

What should I include so if the user input is something like below, it would still be accepted:

puts "What's your favorite programming language?"
language = gets.chomp.capitalize

case language
when "Ruby" then puts "Ruby is great for web apps!"
when "Python" then puts "Python is great for science."
when "JavaScript" then puts "JavaScript makes websites awesome."
when "HTML" then puts "HTML is what websites are made of!"
when "CSS" then puts "CSS makes websites pretty."
  puts "I don't know that code!"


Normalise the capitalisation only for the comparison, there's no need/reason to change the output as well

You can also use regex for your when's, adding the flag i causes the pattern to ignore case

irb(main):001:0> option = 'heLlO'
=> "heLlO"
irb(main):002:0> case option
irb(main):003:1> when /hello/i
irb(main):004:1>   puts 'matched pattern /hello/i'
irb(main):005:1> end
matched pattern /hello/i


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.