Open In App

Ruby | Array class empty?() operation

Last Updated : 08 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Ruby code for empty?() method
  
# declaring array
a = [18, 22, 33, nil, 5, 6]
  
# declaring array
b = [1, 4, 1, 1, 88, 9]
  
# declaring array
c = []
  
# empty?
puts "empty? : #{a.empty?()}\n\n"
  
# empty?
puts "empty? : #{b.empty?()}\n\n"
  
# empty?
puts "empty? : #{c.empty?()}\n\n"


Output :

empty? : false

empty? : false

empty? : true

Code #2 : Example for empty?() method




# Ruby code for empty?() method
# declaring array
a = ["abc", "nil", "dog"]
  
# declaring array
b = ["cow", nil, "dog"]
  
# declaring array
c = ["cat", nil, nil]
  
# empty?
puts "empty? : #{a.empty?()}\n\n"
  
# empty?
puts "empty? : #{b.empty?()}\n\n"
  
# empty?
puts "empty? : #{c.empty?()}\n\n"


Output :

empty? : false

empty? : false

empty? : false


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads