Ruby Array find_first object? Ruby Array find_first object? arrays arrays

Ruby Array find_first object?


Either I don't understand your question, or Enumerable#find is the thing you were looking for.


use array detect method if you wanted to return first value where block returns true

[1,2,3,11,34].detect(&:even?) #=> 2OR[1,2,3,11,34].detect{|i| i.even?} #=> 2

If you wanted to return all values where block returns true then use select

[1,2,3,11,34].select(&:even?)  #=> [2, 34]


Guess you just missed the find method in the docs:

my_array.find {|e| e.satisfies_condition? }