how to check if an array is empty? in ruby

In Ruby, you can check if an array is empty using the empty? method. This method returns true if the array is empty, and false otherwise.

Here's an example:

main.rb
array = []

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

Alternatively, you can check the size of the array using the length method. If the length is 0, then the array is empty.

Here's an example using the length method:

main.rb
array = []

if array.length == 0
  puts "Array is empty"
else
  puts "Array is not empty"
end
94 chars
8 lines

Both methods will give you the same result.

Remember to replace array with the actual array variable you want to check.

related categories

gistlibby LogSnag