What’s a fast way to verify whether all elements of an enumerable satisfy a certain condition? I guess logically it would be like:
elements = [e1, e2, e3, ...]
return (condition on e1) && (condition on e2) && (condition on e3) && ...
For example, if I had an array of integers, and I wanted to answer the question “Are all integers odd?”
I can always iterate over each value, check whether it’s true, and then return false when one of them returns false, but is there a better way to do it?
You can use the
all?function from the Enumerable mix-in.Or, as pointed out in the comments, you could also use
odd?if you’re looking specificially for odd values.