I’m not exactly sure how I’ve missed Enumerable#all? and #Enumerable#any?, but mark them as yet two more reasons to love Ruby. Both methods pass all elements of the collection to a block and they return true or false based on the collection’s matchy-ness. Examples follow:
["Joe Purdy", "Jonathon Coulton"].all? { |artist| artist.rocks? } #=>true ["Fortran", "BASIC"].any? { |language| language.rocks? } #=>false
Oh, you want a serious example. Sheesh. Let’s see if we can find out if there are any even numbers in a collection:
[0,1,2,3,4,5].any? { |x| x % 2 == 0 } #=> true
Cool. Now, are all of them even?
[0,1,2,3,4,5].all? { |x| x % 2 == 0 } #=> false
See? Aren’t they just the coolest?