How do I check if there's a nil set or not in

2019-02-05 10:11发布

If I set some nil values to an array, I don't seem to be able to check for them. I have tried any? and empty?.

array = [nil, nil, nil]
#=> [nil, nil, nil]

array[1].any?
#=> "NoMethodError: undefined method `any?' for nil:NilClass"

标签: ruby arrays null
5条回答
We Are One
2楼-- · 2019-02-05 10:20

If you want to check if the array includes nil:

[nil, 1, 2].include?(nil) #=> true
查看更多
The star\"
3楼-- · 2019-02-05 10:27
array = [nil, nil, nil]
#=> [nil, nil, nil]
array[1].nil?
#=> true
查看更多
男人必须洒脱
4楼-- · 2019-02-05 10:29
# check if there are any truthy values in the array
[nil, nil, nil].any? #=> false
[nil, true, nil].any? #=> true

# check if all values are truthy
[true,Duck.new,2,'yes'].all? #=> true
[nil, false].all? #=> false

# check if none values are truthy
[nil,false,nil].none? #=> true
[Math::PI,nil,false].none? #=> false

All this methods accepts an optional block for the boolean evaluation.

查看更多
我想做一个坏孩纸
5楼-- · 2019-02-05 10:33

any? iterates over a container, like an array, and looks at each element passed to it to see if it passes a test. If one does, the loop stops and true is returned:

ary = [nil, 1]

ary.any?{ |e| e.nil? } # => true

The documentation explains this well:

Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil. If the block is not given, Ruby adds an implicit block of { |obj| obj } that will cause any? to return true if at least one of the collection members is not false or nil.

%w[ant bear cat].any? { |word| word.length >= 3 } #=> true
%w[ant bear cat].any? { |word| word.length >= 4 } #=> true
[nil, true, 99].any?                              #=> true

any? is one of a number of tests that can be applied to an array to determine if there are none?, any?, one?, or all?.

ary.one?{ |e| e == 1 } # => true
ary.none?{ |e| e == 2 } # => true
ary.all? { |e| e.nil? } # => false

Your code fails because you're trying to use a non-existing any? method for a nil value, hence the error you got: "NoMethodError: undefined method `any?' for nil:NilClass"

ary[0] # => nil
ary.first # => nil
ary.first.respond_to?(:'any?') # => false

You have to pay attention to what you're doing. ary[0] or array.first returns the element at that array index, not an array.

empty? only checks to see if the container has elements in it. In other words, does it have a size > 0?

ary.empty? # => false
ary.size == 0 # => false
ary.size > 0 # => true
[].empty? # => true
[].size == 0 # => true
[].size > 0 # => false
查看更多
Luminary・发光体
6楼-- · 2019-02-05 10:35

You can turn nil values into booleans this way to check for them

array = [nil, nil, nil]
#=> [nil, nil, nil]
!array[1]
#=> true
!!array[1]
#=>false

However, using any? checks for the existence of something. You can use

array.any? {|item| !item }
#=> true

To check if there are any values that evaluate to false as a nil value does. Be careful if you might have a false value in your array though because it will also be evaluated as true using this this method.

Or you could just use:

array.any? { |item| item.nil? }

As other posters have suggested

Your problem is that you are calling the any? method on nil and not on an array.

查看更多
登录 后发表回答