How can I use Array#delete while iterating over the array?

前端 未结 4 819
轻奢々
轻奢々 2020-12-24 10:21

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         


        
相关标签:
4条回答
  • 2020-12-24 10:50

    a.delete_if { |x| x >= 3 }

    See method documentation here

    Update:

    You can handle x in the block:

    a.delete_if do |element|
      if element >= 3
        do_something_with(element)
        true # Make sure the if statement returns true, so it gets marked for deletion
      end
    end
    
    0 讨论(0)
  • 2020-12-24 10:54

    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.

    a.delete_if {|x| x >= 3 }
    

    or

    a.reject! {|x| x >= 3 }
    

    will both work fine.

    0 讨论(0)
  • 2020-12-24 10:57

    I asked this question not long ago.

    Deleting While Iterating in Ruby?

    It's not working because Ruby exits the .each loop when attempting to delete something. If you simply want to delete things from the array, delete_if will work, but if you want more control, the solution I have in that thread works, though it's kind of ugly.

    0 讨论(0)
  • 2020-12-24 11:04

    You don't have to delete from the array, you can filter it so:

    a = [1, 2, 3, 4, 5]
    
    b = a.select {|x| x < 3}
    
    puts b.inspect # => [1,2]
    
    b.each {|i| puts i} # do something to each here
    
    0 讨论(0)
提交回复
热议问题