Enumerable#none?
What does none? do?
none? returns true if no 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 all elements are falsy (i.e., whether no element is truthy).
The method short-circuits — it stops iterating as soon as it finds an element that satisfies the condition, making it efficient for large collections.
Basic Usage
With a block
[1, 2, 3].none? { |n| n > 5 } # => true
[1, 2, 3].none? { |n| n > 2 } # => false (3 > 2)
Without a block
When called without a block, none? checks for falsy values. Only nil and false are falsy — everything else is truthy:
[1, 2, 3].none? # => false (has truthy values)
[false, nil].none? # => true
[false, nil, 1].none? # => false (1 is truthy)
[].none? # => true (empty collection)
This makes none? useful for checking whether a collection is empty or contains no meaningful content.
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].none?(Integer) # => false (has integers)
[1, 2, 3, 4].none?(String) # => true (no strings)
["a", "b", "c"].none?(/x/) # => true (no x's)
["a", "b", "c"].none?(/b/) # => false (has b's)
This is equivalent to:
[1, 2, 3, 4].none? { |n| Integer === n } # => false
Practical Examples
Checking that no number exceeds a limit
prices = [9.99, 14.99, 5.99, 22.50]
if prices.none? { |p| p > 100 }
puts "All prices are under $100"
else
puts "Some prices exceed $100"
end
# => All prices are under $100
Validating input
def all_positive?(numbers)
numbers.none? { |n| n <= 0 }
end
all_positive?([1, 2, 3, 4]) # => true
all_positive?([1, -2, 3]) # => false
Checking for absence of a pattern
emails = ["alice@example.com", "bob@test.org", "charlie@domain.net"]
emails.none? { |email| email.end_with?(".gov") } # => true
emails.none? { |email| email.include?("admin") } # => true
Avoiding nil before processing
def log_message(msg)
return if messages.none?
# process messages...
end
log_message([]) # => nil (early return)
log_message(["hi"]) # => continues
Short-Circuit Evaluation
none? stops as soon as it finds an element that satisfies the block’s condition (returns true). This matters when the block has side effects or when performance is critical:
# Stops after finding the first matching element
[1, 2, 3, 4].none? do |n|
puts "Checking #{n}" # This prints only for 1, 2, 3
n > 2
end
# => false
# Output:
# Checking 1
# Checking 2
# Checking 3
Empty collections
none? always returns true for an empty collection, regardless of the block:
[].none? { |x| x > 5 } # => true
[].none? # => true
Performance Considerations
Lazy evaluation
Because none? short-circuits, the worst case (no element matches) requires checking every element, but the best case (first element matches) exits immediately:
# Worst case: O(n) - checks all elements
[1, 2, 3].none? { |n| n > 100 } # => true, checks all 3
# Best case: O(1) - stops at first element
[100, 200, 300].none? { |n| n > 50 } # => false, stops at first element
Arrays vs Sets
For membership testing, none? over an array performs linear search. If you frequently check whether any element matches a value, consider a Hash or Set:
require "set"
blocked_tags = Set.new(["spam", "banned", "deleted"])
# Array: O(n) linear search each time
content_tags = ["ruby", "python", "go"]
content_tags.none? { |tag| tag == "spam" } # => true
# Set: O(1) lookup
blocked_tags.none? { |tag| tag == "spam" } # => false
Relationship to any? and all?
none? is the complement of any? and the inverse of all?:
| Method | Returns true when… |
|---|---|
any? | At least one element satisfies the condition |
all? | Every element satisfies the condition |
none? | No element satisfies the condition |
# none? vs any?
[1, 2, 3].none? { |n| n > 5 } # => true (no element is > 5)
[1, 2, 3].any? { |n| n > 5 } # => false
# none? vs all?
[1, 2, 3].none? { |n| n > 2 } # => false (3 > 2, so an element does satisfy)
[1, 2, 3].all? { |n| n > 2 } # => false (1 and 2 don't satisfy)
# Logical equivalences
[1, 2, 3].none? { |x| condition }
# is equivalent to:
![1, 2, 3].any? { |x| condition }
You can often choose between any? and none? based on readability:
# These are equivalent:
collection.none? { |x| x.nil? }
!collection.any? { |x| x.nil? }
See Also
- /reference/enumerable/enumerable-any/ — Returns true if any element satisfies the condition
- /reference/enumerable/enumerable-all/ — Returns true if all elements satisfy the condition
- /reference/enumerable/enumerable-find/ — Returns the first element that satisfies a condition