This question already has an answer here:
I would like to know if there's anything except nil
values in an array.
arr = [nil, nil, nil, nil] # => true
arr = [nil, 45, nil, nil] # => false
There could be any values of any types (not only 45
).
Use the
Enumerable#all?
method:Or
As @Stefan suggested,
you could do
arr.compact.empty?
,compact
gets rid of all thenil
for youyou could read here at ruby guides to find about all the methods on
Array
classYou can also use #any? method for array
From the documentation:
Note: This won't work if
false
boolean values are present.Other options would be: