In Ruby, you can check if an array is empty using the empty?
method.
In this example,
numbers
with some elements.empty?
method and store the result in the variable is_empty
.numbers = [1, 2, 3]
is_empty = numbers.empty?
puts "Array is empty: #{is_empty}"
Array is empty: false
In this example,
empty_array
.empty?
method and store the result in the variable is_empty
.empty_array = []
is_empty = empty_array.empty?
puts "Array is empty: #{is_empty}"
Array is empty: true
In this tutorial, we learned How to check if an Array is Empty in Ruby language with well detailed examples.