Consider an array having only one value nil
array = [nil]
Is there any better way to check whether an array is nil or not, like array.nil??
This works:
array == [nil]
But what if there are multiple nil values in the array?
array = [nil, nil, nil]
Below is the requirement:
array = [1, 2] if array.nil?
array.nil? should also give 'true' when array = [nil, nil, nil]
arrayarenilor if all elements ofarrayarenil?array.all?(&:nil?). Says exactly what you're trying to do.nilvalues come from? What do they indicate?array.compact.empty?, butall?(&:nil?)is both idiomatic and potentially faster since it would terminate on the first nonnilvalue.