可以使用any来检查数组是否为空吗? [英] Is it ok to use `any?` to check if an array is not empty?
本文介绍了可以使用any来检查数组是否为空吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 any?
方法检查数组是否为 not 是否为空?
Is it bad to check if an array is not empty by using any?
method?
a = [1,2,3]
a.any?
=> true
a.clear
a.any?
=> false
还是使用除非a.empty更好?
吗?
推荐答案
任何?
在某些情况下与不为空?
不同.
any?
isn't the same as not empty?
in some cases.
>> [nil, 1].any?
=> true
>> [nil, nil].any?
=> false
从文档中:
如果未给出该块,则Ruby添加{| obj |的隐式块obj}(即有吗如果至少集合成员之一不是错误或无).
If the block is not given, Ruby adds an implicit block of {|obj| obj} (that is any? will return true if at least one of the collection members is not false or nil).
这篇关于可以使用any来检查数组是否为空吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文