Enumerable#all?

Updated April 1, 2026 · Enumerable
ruby enumerable all? stdlib

What does all? do?

all? returns true if every element in the collection satisfies the given condition. If a block is provided, it tests each element against the block’s condition. If no block is given, it checks whether every element is truthy (i.e., not nil or false).

The method short-circuits — it stops iterating as soon as it finds an element that fails the condition, making it efficient for large collections.

Basic Usage

With a block

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

Without a block

When called without a block, all? checks for truthy values. Only nil and false are falsy — everything else is truthy:

[1, 2, 3].all?                  # => true
[true, 1, "hello"].all?         # => true
[true, false, 1].all?           # => false
[false, nil].all?               # => false

Empty collections

An important edge case: all? returns true for empty collections. This is logically sound — an empty set vacuously satisfies any condition:

[].all?                         # => true
[].all? { |x| x > 5 }          # => true

This behavior can be surprising, so always consider how your code handles empty input.

With a Predicate Argument

You can pass a pattern object as an argument. The method treats it like a block that calls === on the pattern:

[1, 2, 3, 4].all?(Integer)        # => true
[1, 2, "three", 4].all?(Integer)  # => false
[1, 2, 3, 4].all?(String)         # => false
["a", "b", "c"].all?(String)      # => true

This is equivalent to:

[1, 2, 3, 4].all? { |n| Integer === n }  # => true

Practical Examples

Validating all numbers meet a condition

scores = [85, 90, 78, 92, 88]

if scores.all? { |s| s >= 70 }
  puts "All students passed!"
else
  puts "Some students need improvement"
end
# => All students passed!

Checking string constraints

usernames = ["alice", "bob", "charlie"]

usernames.all? { |name| name.length >= 3 }   # => true
usernames.all? { |name| name.start_with?("a") }  # => false

Ensuring required keys are present

config = { timeout: 30, retries: 3, debug: false }

required_keys = [:timeout, :retries, :debug]
config.keys.all? { |key| required_keys.include?(key) }  # => true

Type validation

def all_integers?(array)
  array.all?(Integer)
end

all_integers?([1, 2, 3, 4])    # => true
all_integers?([1, 2, "three"]) # => false

Short-Circuit Evaluation

all? stops as soon as it finds an element that fails the condition. This matters when the block has side effects or when performance is critical:

# Stops at the first failing element
[1, 2, 3, 4].all? do |n|
  puts "Checking #{n}"
  n < 3
end
# => false
# Output:
# Checking 1
# Checking 2
# Checking 3

Performance Considerations

Lazy evaluation

Because all? short-circuits, the best case (first element fails) exits immediately, but the worst case (all elements pass) requires checking every element:

# Worst case: O(n) — all elements pass
[1, 2, 3].all? { |n| n < 10 }  # => true, checks all 3

# Best case: O(1) — first element fails
[10, 20, 30].all? { |n| n < 5 }  # => false, stops at first element

Arrays vs Sets

For membership testing, all? over an array performs linear search. If you frequently check whether all elements match a value, consider using more targeted structures:

# Array: O(n) linear search
data = [1, 2, 3, 4, 5]
data.all? { |n| n.is_a?(Integer) }  # => true

# More efficient for specific use cases
valid_statuses = Set.new([:active, :pending, :completed])
[data.all? { |item| valid_statuses.include?(item.status) }]

Relationship to any? and none?

all? is the opposite of any? and complements none?:

MethodReturns true when…
all?Every element satisfies the condition
any?At least one element satisfies the condition
none?No element satisfies the condition
# all? vs any?
[1, 2, 3].all? { |n| n > 0 }    # => true  (all > 0)
[1, 2, 3].any? { |n| n > 2 }    # => true  (3 > 2)

# all? vs none?
[1, 2, 3].all? { |n| n > 5 }    # => false (none > 5, but all? returns false)
[1, 2, 3].none? { |n| n > 5 }   # => true  (no element is > 5)

You can often choose between all? and none? based on readability:

# These are equivalent:
collection.all? { |x| x.valid? }
collection.none? { |x| !x.valid? }

Edge Cases

Mixed truthy/falsy values

[1, "hello", true, []].all?      # => true  (all truthy)
[1, "hello", false, []].all?     # => false (false is falsy)
[1, "hello", nil, []].all?       # => false (nil is falsy)

Special objects

Remember that false and nil are the only falsy values in Ruby. Everything else is truthy:

[0, "", [], {}].all?             # => true (0, "", [], {} are all truthy)

See Also