Ruby | Array class empty?() operation
Last Updated :
08 Jan, 2020
Array#empty?() : empty?() is a Array class method which checks if the array is empty or not.
Syntax: Array.empty?()
Parameter: Array
Return: true – if no element is present in the array; otherwise false
Code #1 : Example for empty?() method
a = [18, 22, 33, nil, 5, 6]
b = [1, 4, 1, 1, 88, 9]
c = []
puts "empty? : #{a.empty?()}\n\n"
puts "empty? : #{b.empty?()}\n\n"
puts "empty? : #{c.empty?()}\n\n"
|
Output :
empty? : false
empty? : false
empty? : true
Code #2 : Example for empty?() method
a = ["abc", "nil", "dog"]
b = ["cow", nil, "dog"]
c = ["cat", nil, nil]
puts "empty? : #{a.empty?()}\n\n"
puts "empty? : #{b.empty?()}\n\n"
puts "empty? : #{c.empty?()}\n\n"
|
Output :
empty? : false
empty? : false
empty? : false
Share your thoughts in the comments
Please Login to comment...