I have an array of hashed names and emails, like this:
array = [{"email"=>"[email protected]", "name"=>"Test"},
{"email"=>"[email protected]", "name"=>"Test A"},
{"name"=>"Test B", "email"=>"[email protected]"},
{"email"=>"[email protected]", "name"=>"Test C"},
{"name"=>"Test D", "email"=>"[email protected]"},
{"email"=>"[email protected]"},
{"name"=>"Test F", "email"=>"[email protected]"}]
I want to filter out certain emails in a "blacklist" array. The following works, but it's too verbose.
blacklist = ["@test.com", "@gmail.com"]
na = array
blacklist.each do |b|
na = na.reject{ |e| e["email"].include?(b) }
end
# na => [{"email"=>"[email protected]", "name"=>"Test C"}, {"name"=>"Test D", "email"=>"[email protected]"}]
Can someone help me by putting this into a sexy Ruby one-liner?
One more suggestion :)
array.reject { |h| blacklist.any? { |b| h["email"].include? b } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With