[ACCEPTED]-Built in method for testing either nil or empty array?-ruby
Accepted answer
Without Rails or ActiveSupport,
array.to_a.empty?
0
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
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.
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
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.