Delete array elements according to certain criteria

What is the best and way to do this: I have two arrays:

a=[['a','one'],['b','two'],['c','three'],['d','four']] 

and b=['two','three']

I want to remove nested arrays inside a that include elements in b to get the following:

 [['a','one']['d','four'] 

Thanks.

+6
arrays ruby
source share
3 answers
 a = [['a','one'],['b','two'],['c','three'],['d','four']] b = ['two','three'] a.delete_if { |x| b.include?(x.last) } pa # => [["a", "one"], ["d", "four"]] 
+14
source share

rassoc to the rescue!

  b.each {|el| a.delete(a.rassoc(el)) } 
+5
source share
 a=[['a','one'],['b','two'],['c','three'],['d','four']] b=['two','three'] result=a.reject { |e| b.include?(e.first) or b.include?(e.last) } # result => [["a", "one"], ["d", "four"]] 
+2
source share

All Articles