From: Josselin Date: 2007-06-04T00:50:24+09:00 Subject: Re: delete in Array (if exist) and count On 2007-06-03 16:57:43 +0200, "Todd Benson" said: > On 6/3/07, Josselin wrote: >> I am trying to delete an element from an array if it exists and count >> the number of item afterwards >> >> I wrote >> >> anArray = [1,2,3,4,5,6,7,8,9] >> anArray.size >> => 9 >> anArray.map {|e| e if e != 4}.compact >> => [1, 2, 3, 5, 6, 7, 8, 9] >> anArray.map {|e| e if e != 4}.compact.size >> => 8 >> >> >> is ther a better way to do it... it it's enough ? (learning always how >> to write better code..) > > Array#delete works also with no need for a block (i.e. you're only > checking for equality) > > anArray = [1,2,3,4,5,6] > p anArray.delete(4) > p anArray.size I checked anArray.delete(0) => nil then anArray.delete(0).size NoMethodError: undefined method `size' for nil:NilClass as I tried to write it in one line.... thanks joss