0
0
Rubyprogramming~3 mins

Why Any?, all?, none? predicates in Ruby? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could ask big questions about your data with just one simple line of code?

The Scenario

Imagine you have a list of tasks and you want to check if any task is done, if all tasks are done, or if none are done. Doing this by checking each task one by one by hand is tiring and slow.

The Problem

Manually checking each item means writing long loops and many if-statements. It's easy to make mistakes, forget a case, or write confusing code that's hard to read and fix.

The Solution

Ruby's any?, all?, and none? methods let you ask these questions simply and clearly. They check the whole list for you and return true or false quickly.

Before vs After
Before
done = false
for task in tasks
  if task.done?
    done = true
    break
  end
end
After
done = tasks.any?(&:done?)
What It Enables

You can write clean, easy-to-understand code that quickly answers questions about collections without extra work.

Real Life Example

Checking if any user in a list has admin rights before allowing access to a special page.

Key Takeaways

Manually checking collections is slow and error-prone.

any?, all?, and none? simplify these checks.

They make code shorter, clearer, and easier to maintain.