Recall & Review
beginner
What does the
any? method do in Ruby?The
any? method checks if at least one element in a collection meets a condition. It returns true if any element matches, otherwise false.Click to reveal answer
beginner
How does the
all? method work in Ruby?The
all? method returns true only if every element in the collection meets the given condition. If any element does not match, it returns false.Click to reveal answer
beginner
Explain the purpose of the
none? method in Ruby.The
none? method returns true if no elements in the collection meet the condition. It returns false if at least one element matches.Click to reveal answer
beginner
What will
[1, 2, 3].any? { |n| n > 2 } return?It will return
true because at least one element (3) is greater than 2.Click to reveal answer
beginner
What is the result of
[1, 2, 3].all? { |n| n < 4 }?It returns
true because all elements (1, 2, 3) are less than 4.Click to reveal answer
What does
none? return when no elements satisfy the condition?✗ Incorrect
none? returns true if no elements meet the condition.Which method returns
true if at least one element matches the condition?✗ Incorrect
any? returns true if any element matches.What will
[4, 5, 6].all? { |n| n > 3 } return?✗ Incorrect
All elements are greater than 3, so
all? returns true.If
any? returns false, what can you say about the elements?✗ Incorrect
any? returns false only if no elements match.Which method would you use to check if every element is even?
✗ Incorrect
all? checks if all elements satisfy the condition.Describe how
any?, all?, and none? differ in checking conditions on collections.Think about how many elements need to satisfy the condition for each method.
You got /3 concepts.
Give an example of when you would use
none? in a Ruby program.Consider situations where you want to confirm absence of a condition.
You got /3 concepts.