.empty?
is a method in Ruby that checks if an array is empty or not. .empty?
returns a Boolean value true
or false
. The method returns true
if the array is actually empty; otherwise, it returns false
.
array.empty?
.empty?
does not accept any parameters. It is only called on an array.
The .empty?
method returns true
if the array is empty; otherwise, it returns false
.
In the code below, we create arrays and call the empty?
method on them. Then, we print the returned values to the console.
# creating arraysarray1 = [1, 2, 3, 4, 5]array2 = ["a", "b", "c", "d", "e"]array3 = ["Ruby on Rails!", "Java", "C++", "Python", "Javascript" ]array4 = [nil, nil]array5 = []array6 = [false, true, 1, 0]# check if arrays are emptya = array1.empty?b = array2.empty?c = array3.empty?d = array4.empty?e = array5.empty?f = array6.empty?# print returned values to consoleputs "#{array1} is empty? : #{a}" # falseputs "#{array2} is empty? : #{b}" # falseputs "#{array3} is empty? : #{c}" # falseputs "#{array4} is empty? : #{d}" # falseputs "#{array5} is empty? : #{e}" # trueputs "#{array6} is empty? : #{f}" # false