Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Array#any?
() -> bool
(::RBS::Ops::_CaseEqual[Elem, ::boolish] pattern) -> bool
() { (Elem element) -> ::boolish } -> bool
any? → true or false
any?(object) → true or false
any? {|element| ... } → true or false
array.c
View on GitHub ↗
static VALUE
rb_ary_any_p(int argc, VALUE *argv, VALUE ary)
{
rb_len_t i, len = RARRAY_LEN(ary);
rb_check_arity(argc, 0, 1);
if (!len) return Qfalse;
if (argc) {
if (rb_block_given_p()) {
rb_warn("given block not used");
}
for (i = 0; i < RARRAY_LEN(ary); ++i) {
if (RTEST(rb_funcall(argv[0], idEqq, 1, RARRAY_AREF(ary, i)))) return Qtrue;
}
}
else if (!rb_block_given_p()) {
for (i = 0; i < len; ++i) {
if (RTEST(RARRAY_AREF(ary, i))) return Qtrue;
}
}
else {
for (i = 0; i < RARRAY_LEN(ary); ++i) {
if (RTEST(rb_yield(RARRAY_AREF(ary, i)))) return Qtrue;
}
}
return Qfalse;
}
Returns whether for any element of self, a given criterion is satisfied.
With no block and no argument, returns whether any element of self is truthy:
Example
[nil, false, []].any? # => true # Array object is truthy. [nil, false, {}].any? # => true # Hash object is truthy. [nil, false, ''].any? # => true # String object is truthy. [nil, false].any? # => false # Nil and false are not truthy.
With argument object given, returns whether object === ele for any element ele in self:
Example
[nil, false, 0].any?(0) # => true [nil, false, 1].any?(0) # => false [nil, false, 'food'].any?(/foo/) # => true [nil, false, 'food'].any?(/bar/) # => false
With a block given, calls the block with each element in self; returns whether the block returns any truthy value:
Example
[0, 1, 2].any? {|ele| ele < 1 } # => true [0, 1, 2].any? {|ele| ele < 0 } # => false
With both a block and argument object given, ignores the block and uses object as above.
Special case: returns false if self is empty (regardless of any given argument or block).
Related: see Methods for Querying.