×

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 case statement – Find output programs (set 2)

Last Updated : December 15, 2025

Program 1

case 50
    when 11...20
       puts "Number is within the range of 11 to 20"; 
    when 21...30
       puts "Number is within the range of 21 to 30"; 
    when 31...40
       puts "Number is within the range of 31 to 40"; 
    when 41...50
       puts "Number is within the range of 41 to 50"; 
    else 
        puts "Unknown number";    
end

Output

Unknown number

Explanation

In the above program, we created case blocks. In the code there is no case-matched because when we used "..." for the range it excludes the last digit in the range. That's why the else part gets executed and printed "Unknown number" message.


Program 2

case 49
    when 11...20
       puts "Number is within the range of 11 to 20"; 
       break;
    when 21...30
       puts "Number is within the range of 21 to 30"; 
       break;
    when 31...40
       puts "Number is within the range of 31 to 40"; 
       break;
    when 41...50
       puts "Number is within the range of 41 to 50"; 
       break;
    else 
        puts "Unknown number";    
end

Output

HelloWorld.rb:4: Invalid break
HelloWorld.rb: compile error (SyntaxError)

Explanation

The above program will generate a syntax error because we cannot use the break statement in the when block.


Program 3

case 49
    when 11...20
       puts "Number is within the range of 11 to 20"; 
       
    when 21...30
       puts "Number is within the range of 21 to 30"; 
       
    when 31...40
       puts "Number is within the range of 31 to 40"; 
       
    when 41...50
       puts "Number is within the range of 41 to 50"; 
       
    default 
        puts "Unknown number";    
end

Output

Number is within the range of 41 to 50

HelloWorld.rb:14:in `<main>': undefined local variable or method `default' 
for main:Object (NameError)

Explanation

In the above program, we created case blocks. Here, case 41..50 matched the given number with range, and print the appropriate message on the console screen. After that, runtime error gets generated due to default.


Program 4

case 50
    when 10,20,30
       puts "TEN TWENTY THIRTY"; 
       
    when 40,50,60
       puts "FORTY FIFTY SIXTY"; 
       
    when 70,80,90
       puts "SEVENTY EIGHTY NINETY"; 

    else 
        puts "Unknown number";    
end

Output

FORTY FIFTY SIXTY

Explanation

In the above program, we created case blocks. Here, we used multiple values with the when block to match the given value. Then it matched with the "when 40,50,60" block and printed a "FORTY FIFTY SIXTY" message.


Program 5

str = "SUN";

case 
    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

Weekday is SUNDAY

Explanation

In the above program, we created a variable str initialized with "SUN". Then we matched the value of the str variable with the when block and printed the "Weekday is SUNDAY" message.

Ruby Find Output Programs »



Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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