Ruby program to demonstrate the case statement with string

Ruby Example: Write a program to demonstrate the case statement with string.
Submitted by Nidhi, on December 17, 2021

Problem Solution:

In this program, we will demonstrate the case statement with string, here we read a string from the user and match the input string using the case statement and print the appropriate message.

Program/Source Code:

The source code to demonstrate the case statement with string is given below. The given program is compiled and executed successfully.

# Ruby program to demonstrate 
# the case statement with String

print "Enter string: ";
str = gets.chomp;  

case str
when "sun"
    puts "Weekday is SUNDAY";    
when "mon"
    puts "Weekday is MONDAY";    
when "tue"
    puts "Weekday is TUESDAY";    
when "wed"
    puts "Weekday is WEDNESDAY";    
when "thu"
    puts "Weekday is THURSDAY";    
when "fri"
    puts "Weekday is FRIDAY";    
when "sat"
    puts "Weekday is SATURDAY";    
else
    puts "Unknown week";    
end

Output:

Enter string: thu
Weekday is THURSDAY

Explanation:

In the above program, we read a string from the user and match the input string using the case statement, and printed the appropriate message.

Ruby case Statement Programs »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.