Am I missing something in the Array documentation? I have an array which contains up to one object satisfying a certain criterion. I\'d like to efficiently find that objec
use array detect method if you wanted to return first value where block returns true
detect
[1,2,3,11,34].detect(&:even?) #=> 2 OR [1,2,3,11,34].detect{|i| i.even?} #=> 2
If you wanted to return all values where block returns true then use select
select
[1,2,3,11,34].select(&:even?) #=> [2, 34]