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 »


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.