Check for inclusion of a hash against another hash

  • A+

I have an array of hashes like this:

arr = [   { email: '', valid: true },   { email: '', valid: false } ] 

I need to check if email: '' and valid: true both exist in a single hash.

How can I check for such hash in the array without using each loop?

Currently I am doing this:

found = false arr.each do|v|   if v[:email] == '' && v[:valid] == true     found = true     break   end end 


You can use Enumerable#any? that does more or less what you did in your implementation:

> found = arr.any?{|e| e[:email] == '' && e[:valid] } => true 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: