检查数组不是空的:有吗?

时间:2021-03-18 11:08:28

Is it bad to check if an array is not empty by using any? method?

如果数组不是空的,用any来检查是不好的吗?方法?

a = [1,2,3]

a.any?
=> true

a.clear

a.any?
=> false

Or is it better to use unless a.empty? ?

或者最好是使用,除非是空的?吗?

6 个解决方案

#1


187  

any? isn't the same as not empty? in some cases.

任何?不是同样的不空吗?在某些情况下。

>> [nil, 1].any?
=> true
>> [nil, nil].any?
=> false

From the documentation:

从文档:

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

如果没有给出该块,则Ruby会添加一个隐式的{|obj| obj}(那是什么?)如果集合成员中至少有一个不是false或nil,则返回true)。

#2


60  

The difference between an array evaluating its values to true or if its empty.

The method empty? comes from the Array class
http://ruby-doc.org/core-2.0.0/Array.html#method-i-empty-3F

空的方法吗?来自数组类http://ruby-doc.org/core-2.0.0/Array.html#method-i-empty-3F

Its used to check if the array contains something or not. This includes things that evaluate to false such as nil and false.

它用于检查数组是否包含某些内容。这包括计算为false的东西,如nil和false。

>> a = []
=> []
>> a.empty?
=> true
>> a = [nil, false]
=> [nil, false]
>> a.empty?
=> false
>> a = [nil]
=> [nil]
>> a.empty?
=> false

The method any? comes from the Enumerable module.
http://ruby-doc.org/core-2.0.0/Enumerable.html#method-i-any-3F

的方法吗?来自可枚举模块。http://ruby-doc.org/core-2.0.0/Enumerable.html method-i-any-3F

Its used to evaluate if "any" values in the array evaluates to true. Similar methods to this are none? all? and one? where they all just check to see how many times true could be evaluated. which has nothing to do with the count of values found in a array.

它用于计算数组中的“any”值是否为true。类似的方法不是没有吗?所有的吗?和一个吗?它们都只是检查有多少次是正确的。这与数组中找到的值无关。

case 1

案例1

>> a = []
=> []
>> a.any?
=> false
>> a.one?
=> false
>> a.all?
=> true
>> a.none?
=> true

case 2

案例2

>> a = [nil, true]
=> [nil, true]
>> a.any?
=> true
>> a.one?
=> true
>> a.all?
=> false
>> a.none?
=> false

case 3

案例3

>> a = [true, true]
=> [true, true]
>> a.any?
=> true
>> a.one?
=> false
>> a.all?
=> true
>> a.none?
=> false

#3


22  

Prefixing the statement with an exclamation mark will let you know whether the array is not empty. So in your case -

在语句前面加上感叹号可以让您知道数组是否为空。在你的例子中

a = [1,2,3]
!a.empty?
=> true

#4


14  

Avoid any? for large arrays.

避免任何?对于大型数组。

  • any? is O(n)
  • 任何?是O(n)
  • empty? is O(1)
  • 空的吗?是O(1)

any? does not check the length but actually scans the whole array for truthy elements.

任何?不检查长度,而是实际扫描整个数组以寻找真实的元素。

static VALUE
rb_ary_any_p(VALUE ary)
{
  long i, len = RARRAY_LEN(ary);
  const VALUE *ptr = RARRAY_CONST_PTR(ary);

  if (!len) return Qfalse;
  if (!rb_block_given_p()) {
    for (i = 0; i < len; ++i) if (RTEST(ptr[i])) return Qtrue;
  }
  else {
    for (i = 0; i < RARRAY_LEN(ary); ++i) {
        if (RTEST(rb_yield(RARRAY_AREF(ary, i)))) return Qtrue;
    }
  }
  return Qfalse;
}

empty? on the other hand checks the length of the array only.

空的吗?另一方面,只检查数组的长度。

static VALUE
rb_ary_empty_p(VALUE ary)
{
  if (RARRAY_LEN(ary) == 0)
    return Qtrue;
  return Qfalse;
}

The difference is relevant if you have "sparse" arrays that start with lots of nil values, like for example an array that was just created.

如果你的“稀疏”数组以许多nil值开头,比如刚创建的数组,那么这种差异是相关的。

#5


4  

I'll suggest using unlessand blank to check is empty or not.

我建议使用unlessand blank来检查是否为空。

Example :

例子:

unless a.blank?
  a = "Is not empty"
end

This will know 'a' empty or not. If 'a' is blank then the below code will not run.

这将知道“a”是否为空。如果“a”为空,那么下面的代码将不会运行。

#6


0  

I don't think it's bad to use any? at all. I use it a lot. It's clear and concise.

我不认为使用任何东西不好吗?在所有。我经常用它。它简洁明了。

However if you are concerned about all nil values throwing it off, then you are really asking if the array has size > 0. In that case, this dead simple extension (NOT optimized, monkey-style) would get you close.

但是,如果您关心所有的nil值抛出它,那么您实际上是在问数组是否具有>大小。在这种情况下,这个死一般的简单扩展(不是优化的monkey样式)将使您更接近目标。

Object.class_eval do

  def size?
    respond_to?(:size) && size > 0
  end

end

> "foo".size?
 => true
> "".size?
 => false
> " ".size?
 => true
> [].size?
 => false
> [11,22].size?
 => true
> [nil].size?
 => true

This is fairly descriptive, logically asking "does this object have a size?". And it's concise, and it doesn't require ActiveSupport. And it's easy to build on.

这是相当描述性的,逻辑上问“这个对象有大小吗?”它简洁,不需要活动支持。而且它很容易建立。

Some extras to think about:

需要考虑的额外事项:

  1. This is not the same as present? from ActiveSupport.
  2. 这和现在不一样吗?从ActiveSupport这样。
  3. You might want a custom version for String, that ignores whitespace (like present? does).
  4. 您可能想要一个字符串的自定义版本,它会忽略空格(比如present?)做)。
  5. You might want the name length? for String or other types where it might be more descriptive.
  6. 你可能想要名字的长度?对于字符串或其他类型,它可能更具描述性。
  7. You might want it custom for Integer and other Numeric types, so that a logical zero returns false.
  8. 您可能希望对整型和其他数值类型进行自定义,以便逻辑零返回false。

#1


187  

any? isn't the same as not empty? in some cases.

任何?不是同样的不空吗?在某些情况下。

>> [nil, 1].any?
=> true
>> [nil, nil].any?
=> false

From the documentation:

从文档:

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

如果没有给出该块,则Ruby会添加一个隐式的{|obj| obj}(那是什么?)如果集合成员中至少有一个不是false或nil,则返回true)。

#2


60  

The difference between an array evaluating its values to true or if its empty.

The method empty? comes from the Array class
http://ruby-doc.org/core-2.0.0/Array.html#method-i-empty-3F

空的方法吗?来自数组类http://ruby-doc.org/core-2.0.0/Array.html#method-i-empty-3F

Its used to check if the array contains something or not. This includes things that evaluate to false such as nil and false.

它用于检查数组是否包含某些内容。这包括计算为false的东西,如nil和false。

>> a = []
=> []
>> a.empty?
=> true
>> a = [nil, false]
=> [nil, false]
>> a.empty?
=> false
>> a = [nil]
=> [nil]
>> a.empty?
=> false

The method any? comes from the Enumerable module.
http://ruby-doc.org/core-2.0.0/Enumerable.html#method-i-any-3F

的方法吗?来自可枚举模块。http://ruby-doc.org/core-2.0.0/Enumerable.html method-i-any-3F

Its used to evaluate if "any" values in the array evaluates to true. Similar methods to this are none? all? and one? where they all just check to see how many times true could be evaluated. which has nothing to do with the count of values found in a array.

它用于计算数组中的“any”值是否为true。类似的方法不是没有吗?所有的吗?和一个吗?它们都只是检查有多少次是正确的。这与数组中找到的值无关。

case 1

案例1

>> a = []
=> []
>> a.any?
=> false
>> a.one?
=> false
>> a.all?
=> true
>> a.none?
=> true

case 2

案例2

>> a = [nil, true]
=> [nil, true]
>> a.any?
=> true
>> a.one?
=> true
>> a.all?
=> false
>> a.none?
=> false

case 3

案例3

>> a = [true, true]
=> [true, true]
>> a.any?
=> true
>> a.one?
=> false
>> a.all?
=> true
>> a.none?
=> false

#3


22  

Prefixing the statement with an exclamation mark will let you know whether the array is not empty. So in your case -

在语句前面加上感叹号可以让您知道数组是否为空。在你的例子中

a = [1,2,3]
!a.empty?
=> true

#4


14  

Avoid any? for large arrays.

避免任何?对于大型数组。

  • any? is O(n)
  • 任何?是O(n)
  • empty? is O(1)
  • 空的吗?是O(1)

any? does not check the length but actually scans the whole array for truthy elements.

任何?不检查长度,而是实际扫描整个数组以寻找真实的元素。

static VALUE
rb_ary_any_p(VALUE ary)
{
  long i, len = RARRAY_LEN(ary);
  const VALUE *ptr = RARRAY_CONST_PTR(ary);

  if (!len) return Qfalse;
  if (!rb_block_given_p()) {
    for (i = 0; i < len; ++i) if (RTEST(ptr[i])) return Qtrue;
  }
  else {
    for (i = 0; i < RARRAY_LEN(ary); ++i) {
        if (RTEST(rb_yield(RARRAY_AREF(ary, i)))) return Qtrue;
    }
  }
  return Qfalse;
}

empty? on the other hand checks the length of the array only.

空的吗?另一方面,只检查数组的长度。

static VALUE
rb_ary_empty_p(VALUE ary)
{
  if (RARRAY_LEN(ary) == 0)
    return Qtrue;
  return Qfalse;
}

The difference is relevant if you have "sparse" arrays that start with lots of nil values, like for example an array that was just created.

如果你的“稀疏”数组以许多nil值开头,比如刚创建的数组,那么这种差异是相关的。

#5


4  

I'll suggest using unlessand blank to check is empty or not.

我建议使用unlessand blank来检查是否为空。

Example :

例子:

unless a.blank?
  a = "Is not empty"
end

This will know 'a' empty or not. If 'a' is blank then the below code will not run.

这将知道“a”是否为空。如果“a”为空,那么下面的代码将不会运行。

#6


0  

I don't think it's bad to use any? at all. I use it a lot. It's clear and concise.

我不认为使用任何东西不好吗?在所有。我经常用它。它简洁明了。

However if you are concerned about all nil values throwing it off, then you are really asking if the array has size > 0. In that case, this dead simple extension (NOT optimized, monkey-style) would get you close.

但是,如果您关心所有的nil值抛出它,那么您实际上是在问数组是否具有>大小。在这种情况下,这个死一般的简单扩展(不是优化的monkey样式)将使您更接近目标。

Object.class_eval do

  def size?
    respond_to?(:size) && size > 0
  end

end

> "foo".size?
 => true
> "".size?
 => false
> " ".size?
 => true
> [].size?
 => false
> [11,22].size?
 => true
> [nil].size?
 => true

This is fairly descriptive, logically asking "does this object have a size?". And it's concise, and it doesn't require ActiveSupport. And it's easy to build on.

这是相当描述性的,逻辑上问“这个对象有大小吗?”它简洁,不需要活动支持。而且它很容易建立。

Some extras to think about:

需要考虑的额外事项:

  1. This is not the same as present? from ActiveSupport.
  2. 这和现在不一样吗?从ActiveSupport这样。
  3. You might want a custom version for String, that ignores whitespace (like present? does).
  4. 您可能想要一个字符串的自定义版本,它会忽略空格(比如present?)做)。
  5. You might want the name length? for String or other types where it might be more descriptive.
  6. 你可能想要名字的长度?对于字符串或其他类型,它可能更具描述性。
  7. You might want it custom for Integer and other Numeric types, so that a logical zero returns false.
  8. 您可能希望对整型和其他数值类型进行自定义,以便逻辑零返回false。