检查数组不为空:否? [英] Check for array not empty: any?

查看:124
本文介绍了检查数组不为空:否?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是坏的检查,如果一个数组是的不可以空用的?方法?

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

从文档:

如果该块没有给出,加上红宝石
  {的一个隐含的块| OBJ | OBJ}(即
  任何?如果至少将返回true
  集合成员之一是不
  虚假或无)。

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).

这篇关于检查数组不为空:否?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆