From: Robert Klemme Date: 2007-03-23T00:15:04+09:00 Subject: Re: Knocking Lines Out Of A Multiline String On 22.03.2007 16:09, Robert Klemme wrote: > On 22.03.2007 15:43, Andrew Stewart wrote: >> What's a (good!) way to remove lines matching a pattern from a >> multiline string? >> >> For example, I would like to remove lines matching /usr/local/lib from >> the multiline string: >> >> >> /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/test_process.rb:382:in >> `process' >> >> /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/test_process.rb:353:in >> `post' >> test/functional/orders_controller_test.rb:241:in >> `test_should_handle_errors_on_edit' >> >> ..to give: >> >> test/functional/orders_controller_test.rb:241:in >> `test_should_handle_errors_on_edit' >> >> I tried matching the pattern-to-remove with gsub and substituting an >> empty string, but that leaves me with lots of blank lines and not >> really any nearer to the answer. > > Convert it to an array and select like > > >> "foo\nbar\n".to_a.select {|l| /^f/ =~ l} > => ["foo\n"] Bullshit: just use select: >> "foo\nbar\n".select {|l| /^f/ =~ l} => ["foo\n"] Sorry for the noise. robert