check if array is not empty in ruby

To check if an array is not empty in Ruby, you can use the empty? method to check if the array has any elements. The empty? method returns true if the array is empty and false if it has any elements.

Here's an example:

main.rb
array = []

if !array.empty?
  puts "Array is not empty"
else
  puts "Array is empty"
end
90 chars
8 lines

This example defines an empty array array and then uses a conditional statement to check if it's not empty using the ! (not) operator. If the array is not empty, the program prints "Array is not empty". Otherwise, it prints "Array is empty".

gistlibby LogSnag