I have the following array:
array = [{"email"=>"test@test.com", "name"=>"Test"},
{"email"=>"testA@test.com", "name"=>"Test A"},
{"name"=>"Test B", "email"=>"testB@test.com"},
{"email"=>"testC@test.com", "name"=>"Test C"},
{"name"=>"Test D", "email"=>"testD@test.com"},
{"email"=>"testE@test.com"},
{"name"=>"Test F", "email"=>"testF@test.com"}]
I have a list of “blacklist” emails, for instance:
blacklist = ["testC@test.com"]
I want to do something like this:
array - blacklist
# => should remove element {"email"=>"testC@test.com", "name"=>"Test C"}
Surely there is a sexy-Ruby way to do this with .select or something, but I haven’t been able to figure it out. I tried this to no avail:
array.select {|k,v| v != "testC@test.com"} # => returns array without any changes
I think you’re looking for this:
or if you want to use
selectinstead ofreject(perhaps you don’t want to hurt anyone’s feelings):Your
attempt won’t work because array hands the Enumerable blocks a single element and that element will be a Hash in this case, the
|k,v|trick would work ifarrayhad two element arrays as elements though.