×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby program to demonstrate the case statement with string

Last Updated : December 15, 2025

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!

Load comments ↻


Advertisement
Advertisement
Advertisement

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