Q:

Ruby program to check an array is empty or not

belongs to collection: Ruby Arrays Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 arr1arr2arr3. Then we checked created arrays are empty or not using empty?() method and printed the appropriate message.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ruby Arrays Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Ruby program to fetch elements from an array based... >>
<< Ruby program to remove all \'nil\' eleme...