From: Joel VanderWerf Date: 2008-07-07T07:00:57+09:00 Subject: Re: .include? for string Chris Chris wrote: > Hi, > > I have an array a > > => [["abc", "def", "ghi|ghi|kl"], ["jkl", "mno", "ghi|pqr"], ["stu", > "vwx", "yz|abc"]] > > Some strings contain the pipe character. > > I want to delete all the arrows that do NOT contain a certain string. > > I tried this: > > a.delete_if { |x| !x.include? "ghi|ghi" } > > => [] > > Of course, the array I need would be > => [["abc", "def", "ghi|ghi|kl"]] Enumerable#grep can be helpful here. a = [["abc", "def", "ghi|ghi|kl"], ["jkl", "mno", "ghi|pqr"], ["stu", "vwx", "yz|abc"]] a.delete_if { |x| x.grep(/ghi\|ghi/).empty? } p a # ==> [["abc", "def", "ghi|ghi|kl"]] Note that the '|' char needs to be escaped in a regex. Another option: a.delete_if { |x| x.all? { |s| not s.include? "ghi|ghi" }} Closer to the way you phrased the problem, but IMO a little less rubyesque. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407