Evaluating all items in an array in Ruby on Rails

Posted on 12 April 2008

The all method allows you to pass each element of the collection to the block. The result will only be true if none of the items in the array evaluate to false or nil.


>> ["bar", "hi"].all? {|word| word.length > 2}
=> false
>> ["bar", "hi"].all? {|word| word.length >= 2}
=> true

Got something to say?