Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple case/when conditions with || [duplicate]

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!'

like image 898
Dil Azat Avatar asked Dec 06 '16 06:12

Dil Azat


People also ask

How to use case when with multiple conditions?

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.

What is SQL Server case statement with multiple conditions called?

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 -.

How to evaluate multiple expressions in a case statement?

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.

What happens if condition_1 is false in a case statement?

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.


1 Answers

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
like image 173
Vishal G Avatar answered Oct 08 '22 00:10

Vishal G