OneBite.Dev - Coding blog in a bite size

check if array is empty in Ruby

Code snippet on how to check if array is empty in Ruby

  arr.empty?

This code checks if an array is empty or not in Ruby. It does this by using the empty? method on an array, referred to as arr. This method will return true if the array is empty, and false if it is not.

Let’s say arr is an array with 3 strings inside it. In this case, the empty? method will return false, because the array has elements in it.

If arr was an empty array, the empty? method will return true, because there would be no elements in the array.

In conclusion, this line of code is used to check if an array is empty or not by using the empty? method. If it returns true, the array is empty. If it returns false, there are elements in the array.

ruby