1

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]

14
  • You're trying to see if any elements of array are nil or if all elements of array are nil? Commented Feb 15, 2018 at 7:19
  • if all elements of array are nil. Commented Feb 15, 2018 at 7:22
  • 2
    Then I'd go with a modified version of axiac's: array.all?(&:nil?). Says exactly what you're trying to do. Commented Feb 15, 2018 at 7:24
  • Where do the nil values come from? What do they indicate? Commented Feb 15, 2018 at 7:28
  • 1
    Another option would be array.compact.empty?, but all?(&:nil?) is both idiomatic and potentially faster since it would terminate on the first non nil value. Commented Feb 15, 2018 at 8:14

3 Answers 3

3

You can use Array#any? to check if the array contains at least one value that is not false or nil:

array = [1,2] unless array.any?

If array is allowed to contain false and you are interested only in nil then Array#any? needs a block:

array = [1,2] unless array.any?{|i| !i.nil?}

Update

As @mu-is-too-short suggests, a better version is:

array = [1,2] if array.all?(&:nil?)

It does exactly what you need: Enumerable#all? returns true if the block returns a true-ish value for all elements of the array.

Sign up to request clarification or add additional context in comments.

3 Comments

Or array = [1,2] if array.all?(&:nil?) to avoid all the negations.
all? was the method I was looking for but it is not listed in the documentation of Array and I forgot to check in the documentation of Enumerable. Thank you.
There's also none?
2

One option is to uniquify your array and then do what you were already doing:

array.uniq == [nil]

So if array is any amount of nils it will be true. On the other hand:

array.nil?

Checks whether array itself is nil, which is not the same as an array containing nil elements.

11 Comments

Thanks @kabanus, it worked. Also we can use array.uniq.first.nil?
@Rhunal no, because if you have non-nil elements ([nil,nil,2].uniq -> [nil,2]) your test will be invalid.
yes, right, it will fail if array having [2, nil,nil,2]
If array is empty this returns false. That may be OK, but maybe not. The OP has not stated whether array could be empty and, if so, what is to be returned when it is empty. Rhunhl?
@CarySwoveland In my case, I am having array which is not empty, it can contain nil or any value. Because I am passing env var in array, e.g. [ENV['var']]. So it will be never empty.
|
0

One of possible ways is to remove nil values with .compact and check if the result is empty? .

array = [1, 2] if array.compact.empty?

2 Comments

I had added that as a comment below the original post, including a caveat.
@MichaelKohl Sorry, I didn't notice your comment. You are right about readability and probably right about performance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.