×

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 check an array is empty or not

Last Updated : December 15, 2025

Problem Solution

In this program, we will create three arrays of integer elements. Then we will find the created arrays are empty or not using empty?() method.

Program/Source Code

The source code to check an array is empty or not is given below. The given program is compiled and executed successfully.

# Ruby program to check an array is 
# empty or not

arr1 = [10,21,30,nil,41,nil,50];
arr2 = [nil];
arr3 = [];

if arr1.empty?()
    print "Array arr1 is an empty array.\n";
else
    print "Array arr1 is not an empty array.\n";
end

if arr2.empty?()
    print "Array arr2 is an empty array.\n";
else
    print "Array arr2 is not an empty array.\n";
end

if arr3.empty?()
    print "Array arr3 is an empty array.\n";
else
    print "Array arr3 is not an empty array.\n";
end

Output

Array arr1 is not an empty array.
Array arr2 is not an empty array.
Array arr3 is an empty array.

Explanation

In the above program, we created 3 arrays arr1, arr2, arr3. Then we checked created arrays are empty or not using empty?() method and printed the appropriate message.

Ruby Arrays Programs »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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