Ruby program to check an array is empty or not

Ruby Example: Write a program to check an array is empty or not.
Submitted by Nidhi, on January 17, 2022

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 »




Comments and Discussions!

Load comments ↻





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