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