Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Built in method for testing either nil or empty array?

Tags:

ruby

How can I tell if an array is either empty or nil?

like image 939
Blankman Avatar asked Apr 17 '11 16:04

Blankman


People also ask

Is an empty array nil?

|| arr. empty? This will return true of array is empty or the array value is nil.

What is the difference between nil and empty in Ruby?

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.

How check if array is empty C?

✓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.

Is Empty method Ruby?

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.


2 Answers

Without Rails or ActiveSupport,

array.to_a.empty? 
like image 128
sawa Avatar answered Oct 10 '22 02:10

sawa


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 
like image 28
Michael Kohl Avatar answered Oct 10 '22 00:10

Michael Kohl