[ACCEPTED]-Built in method for testing either nil or empty array?-ruby

Accepted answer
Score: 52

Without Rails or ActiveSupport,

array.to_a.empty?

0

Score: 33

There's no built-in Ruby method that does 1 this, but ActiveSupport's blank does:

>> require "active_support/core_ext/object/blank" #=> true
>> nil.blank? #=> true
>> [].blank? #=> true
Score: 13

You can just use the Array#empty? and Object#nil? methods in conjunction 2 with an OR.

arr.nil? || arr.empty?

This will return true of array 1 is empty or the array value is nil.

Score: 5

To check whether array is empty one can 4 use 'empty?' inbuilt method as follows,

array.empty? # returns 3 true/false

To check whether array is nil 2 (If not initialized or set to nil)

array.nil? # returns 1 true/false

More Related questions