From: Stefano Crocco Date: 2009-03-26T02:04:02+09:00 Subject: Re: delete from array while iterating Alle Wednesday 25 March 2009, Tarscher ha scritto: > Hi all, > > I want to delete an element from an array when it matches a condition. > But I first want to print something to the screen when a amùcth is > found. > > I know a.delete_if but can I somehow add that print to the screen next > to the condition? If not , is there an alternative way to delete from > an array while iterating? > > thanks > Stijn In the block you can do anything you want. Just make sure you return a true value or a false value depending on whether the element should be removed or not. Here's an example: a = [1,-1,-2,2,3] a.delete_if do |i| if i < 0 puts "#{i} will be deleted" true else false end end This will return [1,2,3] and give the following output: -1 will be deleted -2 will be deleted I hope this helps Stefano