How can I tell if an array is either empty or nil?
|| arr. empty? This will return true of array is empty or the array value is nil.
nil? is a standard method in Ruby that can be called on all objects and returns true for the nil object and false for anything else. empty? is a standard Ruby method on some objects like Arrays, Hashes and Strings.
✓to check whether an array is empty or not just iterate the elements of the array and compare them with null character '/0'. ✓you can also declare an empty array like this arr[]={}. Then use the sizeof function, if it returns 0 your array is empty.
empty? is a String class method in Ruby which is used to check whether the string length is zero or not. Syntax: str. empty? Parameters: Here, str is the given string which is to be checked.
Without Rails or ActiveSupport,
array.to_a.empty?
There's no built-in Ruby method that does this, but ActiveSupport
's blank
does:
>> require "active_support/core_ext/object/blank" #=> true >> nil.blank? #=> true >> [].blank? #=> true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With