Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Array#all?

array.all? → true or false
array.all? {|element| ... } → true or false
array.all?(obj) → true or false

Returns true if all elements of self meet a given criterion.

With no block given and no argument, returns true if self contains only truthy elements, false otherwise:

Example
[0, 1, :foo].all? # => true
[0, nil, 2].all? # => false
[].all? # => true

With a block given and no argument, calls the block with each element in self; returns true if the block returns only truthy values, false otherwise:

Example
[0, 1, 2].all? { |element| element < 3 } # => true
[0, 1, 2].all? { |element| element < 2 } # => false

If argument obj is given, returns true if obj.=== every element, false otherwise:

Example
['food', 'fool', 'foot'].all?(/foo/) # => true
['food', 'drink'].all?(/bar/) # => false
[].all?(/foo/) # => true
[0, 0, 0].all?(0) # => true
[0, 1, 2].all?(1) # => false

Related: Enumerable#all?

See also #abbrev #any? #| #append also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0