Search code examples
ruby-on-railsarraysrubyhashenumerator

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 = "[email protected]"

email_array is the following:                                                                                                                       
[
    [0] {
        "email" => "[email protected]",
         "name" => "william"
    },
    [1] {
        "email" => "[email protected]",
         "name" => "michael"
    },
    [2] {
        "email" => "[email protected]",
         "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.


Solution

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

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