Check for inclusion of a hash against another hash

  • A+
Category:Languages

I have an array of hashes like this:

arr = [   { email: 'prathab@hotmail.in', valid: true },   { email: 'another@mail.com', valid: false } ] 

I need to check if email: 'prathab@hotmail.in' 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] == 'prathab@hotmail.in' && 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] == 'prathab@hotmail.in' && e[:valid] } => true 

Comment

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