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 »


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.