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

About Paul

Paul works for Kyan web design agency in Surrey, UK as a Ruby on Rails developer.

Follow Paul on Twitter

Email: paulsturgess [at] gmail.com

Read more articles in the archive →

Got something to say?