I have an array that I want to iterate over and delete some of the elements. This doesn\'t work:
a = [1, 2, 3, 4, 5] a.each do |x| next if x < 3 a.del
Another way to do it is using reject!, which is arguably clearer since it has a ! which means "this will change the array". The only difference is that reject! will return nil if no changes were made.
reject!
!
nil
a.delete_if {|x| x >= 3 }
or
a.reject! {|x| x >= 3 }
will both work fine.