배열이 비어 있지 않은지 확인하십시오.
메소드 를 사용하여 배열이 비어 있지 않은지 확인하는 것이 좋지 any?
않습니까?
a = [1,2,3]
a.any?
=> true
a.clear
a.any?
=> false
아니면 사용하는 것이 더 낫 unless a.empty?
습니까?
any?
not empty?
일부 경우 와 동일하지 않습니다 .
>> [nil, 1].any?
=> true
>> [nil, nil].any?
=> false
설명서에서 :
블록이 주어지지 않으면, 루비는 {| obj | obj} (즉, 컬렉션 멤버 중 하나 이상이 false 또는 nil이 아닌 경우 true입니까?)
값을 true로 평가하거나 비어있는 경우 어레이 간의 차이입니다.
이 메소드 empty?
는 Array 클래스 http://ruby-doc.org/core-2.0.0/Array.html#method-i-empty-3F 에서 가져옵니다.
배열에 무언가가 포함되어 있는지 확인하는 데 사용됩니다. 여기에는 nil 및 false와 같이 false로 평가되는 항목이 포함됩니다.
>> a = []
=> []
>> a.empty?
=> true
>> a = [nil, false]
=> [nil, false]
>> a.empty?
=> false
>> a = [nil]
=> [nil]
>> a.empty?
=> false
이 방법 any?
은 열거 가능 모듈에서 제공됩니다.
http://ruby-doc.org/core-2.0.0/Enumerable.html#method-i-any-3F
배열의 "any"값이 true인지 평가하는 데 사용됩니다. 이것과 비슷한 방법이 없습니까? 모두? 그리고 하나? 그들은 모두 참으로 몇 번이나 평가받을 수 있는지 확인합니다. 배열에서 찾은 값의 수와는 관련이 없습니다.
사례 1
>> a = []
=> []
>> a.any?
=> false
>> a.one?
=> false
>> a.all?
=> true
>> a.none?
=> true
사례 2
>> a = [nil, true]
=> [nil, true]
>> a.any?
=> true
>> a.one?
=> true
>> a.all?
=> false
>> a.none?
=> false
사례 3
>> a = [true, true]
=> [true, true]
>> a.any?
=> true
>> a.one?
=> false
>> a.all?
=> true
>> a.none?
=> false
느낌표가있는 문장 앞에 접두사를 붙이면 배열이 비어 있지 않은지 알 수 있습니다. 따라서 귀하의 경우-
a = [1,2,3]
!a.empty?
=> true
any?
큰 배열을 피하십시오 .
any?
이다O(n)
empty?
이다O(1)
any?
길이를 확인하지는 않지만 실제로 전체 배열에서 정확한 요소를 검색합니다.
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?
반면에 배열의 길이 만 확인합니다.
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.
I'll suggest using unless
and blank
to check is empty or not.
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.
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.
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:
- This is not the same as
present?
from ActiveSupport. - You might want a custom version for
String
, that ignores whitespace (likepresent?
does). - You might want the name
length?
forString
or other types where it might be more descriptive. - You might want it custom for
Integer
and otherNumeric
types, so that a logical zero returnsfalse
.
참고URL : https://stackoverflow.com/questions/6245929/check-for-array-not-empty-any
'IT story' 카테고리의 다른 글
JavaScript에서 현재 실행중인 함수의 이름을 얻을 수 있습니까? (0) | 2020.05.26 |
---|---|
1… 1이 10.1로 평가되는 이유는 무엇입니까? (0) | 2020.05.26 |
git-upload-pack : 원격 Git 저장소를 복제 할 때 명령을 찾을 수 없습니다 (0) | 2020.05.26 |
PowerShell에서 로컬 호스트 이름을 얻으려면 어떻게합니까? (0) | 2020.05.26 |
MYSQL OR 대 IN 성능 (0) | 2020.05.26 |