I've been testing this code and it's not working as I expected. Can someone shed some light on this please ?
language = { JS: "Websites", Python: "Science", Ruby: "Web apps" }
puts "What language would you like to know? "
choice = gets.chomp
case choice
when "js" || "JS"
puts "Websites!"
when "Python" || "python"
puts "Science!"
when "Ruby" || "ruby"
puts "Web apps!"
else
puts "I don't know!"
end
, When I put in the first entry it runs, but if I use the latter entry it prints "I don't Know!"
i.e : if I enter 'js' runs, but If I enter 'JS' it throws 'I don't know!'
You can use below example of case when with multiple conditions. Show activity on this post. case when a.REASONID in ('02','03','04','05','06') then case b.CALSOC when '1' then 'yes' when '2' then 'no' else 'no' end else 'no' end Show activity on this post.
SQL case statement with multiple conditions is known as the Search case statement. So, You should use its syntax if you want to get the result based upon different conditions -.
There will be next several WHEN conditions. There are two types of CASE statement, SIMPLE and SEARCHED. You cannot evaluate multiple expressions in a Simple case expression, which is what you were attempting to do.
If condition_1 is true, then the next conditions will be skipped, and CASE execution will be stopped immediately. If condition_1 is false, then the case checks the next condition. This process of checking conditions will continue until the condition is not true. If no conditions are true, then the statement of else block will be executed.
Please do search before asking question, you can get its answer easily in other questions
choice = gets.chomp
case choice
when 'js', 'JS'
puts 'Websites!'
when 'Python', 'python'
puts 'Science!'
when 'Ruby', 'ruby'
puts 'Web apps!'
else
puts "I don't know!"
end
After suggestions
choice = gets.chomp
puts case choice
when 'js', 'JS'
'Websites!'
when 'Python', 'python'
'Science!'
when 'Ruby', 'ruby'
'Web apps!'
else
"I don't know!"
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With