Updated April 3, 2023
Introduction to Ruby Case Statement
Case statement in the Ruby is a way to execute different codes and expressions on the basis of the values provided to the case, case statement in Ruby is similar to basic switch cases in the other programming languages. In case of the switch case in Ruby it has three main components case (it is similar to switch in general concept, it takes the variable input and again it will be used by when), when(this used to check for the condition, it is similar to case of any basic programming language), else(it is for the case when none of condition match, basically it acts as the default).
Syntax:
- The expression here is any value, it can be any string or numeric or any expression which is giving some outputs like (“a”,1==1 etc).
- Then inside the various when block it will keep checking for the expression, and whichever match the condition it will enter into that code block.
- For example if expression is “test” and inside three when block, “final”,”test” and “test3” in this case it will match to the “test” expression of when block and code of this block will executed .
- Finally in case none of expression matching to the when expression block in that case it will go to else block same as default.
case expression(expression may be conditions)
when expression 1(check for matching of expression)
# If match with expression1 this code block will be executed
when expression 2
# If match with expression2 then this code block will be executed
when expression 3
# If match with expression3 then this code block will be executed
else
# In case none of the expression matches then this code block will be executed .
end
How does Case Statement works in Ruby?
Given below is the simple example to see how case statement works in ruby:
- The expression here is any value, it can be any string or numeric or any expression which is giving some outputs like (we have taken the name “Ranjan” inside the expression).
- Then inside the various when block it will keep checking for the expression, and whichever match the condition it will enter into that code block.
- For example if expression is “Ranjan” and inside when blocks, “Ajay”,”Vijay”, “Ranjan”,”Sujit”, in this case it will match to the “Ranjan” expression of when block and code of this block will executed .
- Finally in case none of expression matching to the when expression block in that case it will go to else block same as default.
- It keeps checking for the matching of expression in the various when blocks and if any matches to the when block it will execute them or it will got to else block.
Code:
expression = "Ranjan"
# Here start of the expression
case expression
# First when block to check the expression value
when "Ajay"
puts 'This is the Ajay code block , write logic here to execute'
# Second when block to check the expression value
when "Vijay"
puts 'This is the Vijay code block , write logic here to execute'
# Third when block to check the expression value
when "Ranjan"
puts 'This is the Ranjan code block , write logic here to execute'
# Fourth when block to check the expression value
when "Sujit"
puts 'This is the Sujit code block , write logic here to execute'
else
puts "Nono of the user name matches , so please a general code block here for execution"
end
Output:
Examples of Ruby Case Statement
Given below are the examples:
Example #1
Let us take an example of a student grading system, where some one will enter the marks of the student and it will give output if the student falls under the A,B,C grades. And we know that in colleges and university grades are based on the certain range of the marks. Here in the below example marks enter is 56 and we can see the output in the screen below it showing that this mark’s falls under C grade which is between 45 to 60 .
Code:
# Marks obtained by an student
studentMarks = 56
case studentMarks
# Start of the when block for checking the grade of student..
when 30..45
puts "Student passed with the D grade "
when 45..60
puts "Student passed with C grade"
when 60..75
puts "Student passed with B grade"
when 75..100
puts "Student passed with A grade"
else
puts "Student failed , as the marks obtained by student is very less or not fall under any grade value"
end
Output:
Example #2
Suppose in house there are many switches and every switch has some particular code, like switch 1 will be used for the fan, switch 2 will be used for the AC, switch 3 will be used for bulbs and switch 4 will be used for the fridge. And we wanted to write a code where by entering that particular number we can on off them. In the code below we have assigned 3 to the switch, which means we want to perform the on or off activity with the bulb. Once when block starts it will keep checking for the switch number once it matches it will execute the code block of that when block.
Code:
# We assigned the value for the switch as 3(for bulbs)
switch = 3
# Here when block will start to check the switch type
case switch
when 1
puts "This switch for on or off the fan"
when 2
puts "This switch for on or off the AC"
when 3
puts "This switch for on or off the Bulbs"
when 4
puts "This switch for on or off the fridge"
else
"Enter switch number does not match any available switches"
end
Output:
Conclusion
From this tutorial we learned the basic concepts and practical uses with the help of some real scenario for switch statements, we learned the working and uses of it. We also focus on the its important key components like case, when and else with examples.
Recommended Articles
We hope that this EDUCBA information on “Ruby Case Statement” was beneficial to you. You can view EDUCBA’s recommended articles for more information.