Skip to content

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

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.

See also #all? #append #| #assoc also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0