user2970050 user2970050 - 1 month ago 5
Ruby Question

how do I search for a string value inside an array stored in a hash

I am using ruby on rails and trying to figure out if I can (and/or how I) search for a Hash within an Array for a specific string value? If there is a match (finds Bob), I want it to return True.

```

string_query = "Bob@gmail.com"





email_array is the following:
[
[0] {
"email" => "bill@gmail.com",
"name" => "william"
},
[1] {
"email" => "mike@gmail.com",
"name" => "michael"
},
[2] {
"email" => "Bob@gmail.com",
"name" => "robert"
}
]


```

I see this example on Stack Overflow - but it is numeric. Mine is a string.
How do I get a hash from an array based on a value in the hash?

Many thanks.

Answer

I have not tried this as I am out now, but this could work

string_query = "Bob@gmail.com"
email_array.each do |s|
   if s['email'] == string_query
       #your comparision statements here 
   end
end