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:
p arr.all? { |x| x.nil? }
Or
p arr.all?(&:nil?)
As @Stefan suggested,
p arr.all?(NilClass) #works only for Ruby 2.5
you could do arr.compact.empty?
, compact
gets rid of all the nil
for you
you could read here at ruby guides to find about all the methods on Array
class
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