From: Dave Thomas Date: 2001-04-25T04:10:40+09:00 Subject: [ruby-talk:14169] Re: Ruby way to delete entries from an array Jim Freeze writes: > I have an array where I need to delete several entries. > Here is how I am doing it. > > a = ["cat\n", "dog\n", "bird \n"] > rm = ["cat", "bird"] > > rm.each do |del| > for i in (0...a.size).collect.reverse > a.delete_at(i) if /#{del}/ =~ a[i] > end > end > > Is there a more compact way to do this? Well, this is more compact. The fact that you're using regexp matches makes it a little tricky. pattern = Regexp.new(rm.join('|')) a.delete_if {|entry| entry =~ pattern } In the general case, where the elements of 'rm' might contain meta characters, you'd need the somewhat more cumbersome: pattern = Regexp.new(rm.collect {|str| Regexp.escape(str) } .join('|')) Regards Dave