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


#1



https://www.codecademy.com/courses/ruby-beginner-en-1o8Mb/3/3?curriculum_id=5059f8619189a5000201fbcb#


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:
Html
Javascript
cSS


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."
else
  puts "I don't know that code!"
end


#2

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

#3

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