Ruby is an object-oriented programming language that is general-purpose, dynamic, and reflective. Except for blocks, which have replacements in the form of procs and lambda, everything in Ruby is an object. The goal of Ruby's development was to create a user interface between human programmers and the computational machinery that underpins them.
Ruby's syntax is similar to many other programming languages, including C and Java, making learning easy for Java and C programmers. It runs on various operating systems, including Windows, Mac OS X, and Linux.
Let's talk about the Case statement in Ruby and how to do it in Ruby.So before going deeper into the Case statement let’s first understand a little bit about Conditionals and control structure in ruby.
Conditionals
The conditional is the most frequent type of control structure across all computer languages. This is a means of instructing the computer to only run certain code when a certain condition is met, or conditionally execute some code. The expression that makes up the condition must evaluate to a value other than false or nil in order for it to be satisfied.
Ruby offers a wide variety of conditional expressions. The subsections that follow provide descriptions of the syntactic options. You can use the approach that looks the most elegant for the task at hand when writing Ruby code.
Control structures
Control statements are used in programming languages to regulate the execution of programs depending on specific criteria. These are used to direct the flow of execution to advance and diverge in response to changes in a program's state. The if-else statement is used in Ruby to test the given condition in a similar manner. To read in detail you can refer to our blog on code studio for control structures. Now let’s move to the case statement.
Case Statement
The Case statement in Ruby, like a switch statement in other languages, is a multiway branch statement. It makes it simple to route execution to different areas of code based on the expression's value.
In Ruby, we use 'case' rather than switch'.The Case statement in Ruby is like a switch statement in other languages, matching one statement with many criteria.
3 critical keywords are used in the Case statement in Ruby:
Case: In other programming languages, it's similar to the switch keyword. . It takes the variables that the when keyword will use.
When: In other programming languages, it's analogous to the case keyword. It's only used to match one condition. A single case statement can contain several when statements.
Else: In other programming languages, it's analogous to the default keyword. It's optional, and it'll run if nothing else does.
Syntax of Case statement in Ruby
case expression
[when expression [, expression ...] [then]
Your code ]...
[else
Your code ]
end
The condition will be checked in, and the according will go in the block. Let's see; this will help a flow chart of how Case statements in Ruby work.
Code
# Case statement in Ruby
# Taking Input in Rubuy
day = gets.chomp
# Applying case statement
case day
when "Tuesday"
puts 'Eat Rajma Chawal'
when "Wednesday"
puts 'Eat Butter chicken'
when "Thursday"
puts 'Eat Chole Chawal'
when "Friday"
puts 'Eat Egg curry'
when "Saturday"
puts 'Eat Butter Paneer'
else
puts "This is Fast day so eat fruits"
end
You can also try this code with Online Ruby Compiler
You can see in this code we are printing the meal plan for a person for a week depending on the day that block will execute in this code if the day is Tuesday then it will go in the Tuesday block and will execute the statement written inside so it will print Eat rajma chawal which is Tuesday meal.
Output
Some special cases
The when clause of a case statement may have multiple values and a range of values. let’s understand this with a help of dummy code.
Code
# Ruby program to demonstrate on how to use
# the multiple values in when statement
my_choice = "5"
# using the 'case' statement
case my_choice
# as we can see here 'when' statement
# contains the two values
when "1","2"
puts "Vey Poor"
when "3","4"
puts "Poor"
when "5","6"
puts "Average"
when "7","8"
puts "Good"
when "9","10"
puts "Excellent"
else
"This grade does not exist"
end
You can also try this code with Online Ruby Compiler
As you can see in this piece of code we are checking the marks of students and providing the student the feedback depending on the performance. In this code you can see when the student has received 5 marks out of 10 it will go in the when the block contains multiple values “5” and “6” and will print the average as we can see in the output.
Output
Average
You can also use a case statement without any value. let’s understand this with a help of dummy code.
Code
# Ruby program to demonstrate a case statement without any value
str = "CodingNinjas"
# We can see here case statement has no value
case
# using match keyword to check
when str.match(/\d/)
puts 'String contains numbers'
when str.match(/[a-zA-Z]/)
puts 'String contains letters'
else
puts 'String does not contain numbers ans also letters'
end
You can also try this code with Online Ruby Compiler
In this code as we can see there is no condition in case statement so we will go into the case statement and then it will check the string whether it contains numbers or alphabets and depending on the string it will go in the when condition.
Output
String contains letters
Frequently Asked Questions
Does the Ruby case fall through?
It does not fall through. Ruby doesn't have the same behavior as the Java Programming language for this type of statement.
Does Ruby's case need else?
A single case statement can contain several when statements. Else: In other programming languages, it's analogous to the default keyword. It's optional, and it'll run if nothing else does.
How many cases can a switch statement have?
We can use many statements as we want
What are some common problems with switching statements?
The most serious issue with switch statements is that they can cause code smells. Switch overuse could indicate that you're not using polymorphism appropriately in your code.
Is Ruby Case Sensitive?
Ruby is a case-sensitive language, which means lowercase identifiers in ruby are different from uppercase identifiers. E.g., Like_This is entirely different from LIKE_THIS. Similarly, Start is other than START.
Conclusion
In this article, we have extensively discussed the theory of the Case statement in Ruby along with the basic introduction of Ruby. we also discussed different scenarios of case statements like case statements in method calls and case statements without any value or multiple values.