Ruby array search, returning either a result with a good score or the last result I have an array of hashes, each of which has two attributes: "title" and "Zscore". I want to return either the result of find or the last element. This can be done with:

a = array.select{|i| i["title"] == "some title"}
b = a.find{|i| i["Zscore"].to_f &gt;= 5} || a.last


Is there an elegant way to put the conditional inside the block, something like the following?


array.select{|i| i["title"] == "some title"}.find{|i| i["Zscore"].to_f &gt;= 5 || last}



Any built-in method? Any ideas of one-liner that is nicer than the following?

array.select{|i| i["title"] == "some title"}.find{|i| i["Zscore"].to_f &gt;= 5} ||
array.select{|i| i["title"] == "some title"}.last
You can always define your own custom method to refactor your code in the Array class:

class Array
  def find_or_last(&amp;block)
    find(&amp;block) || last 
  end
end


And use it like this:

array.select { |i| i['title'] == 'some title' }.find_or_last { |i| i['Zscore'].to_f &gt;= 5 }


You can also use procs or lambdas to achieve this.