Deleting While Iterating in Ruby?

后端 未结 2 858
情歌与酒
情歌与酒 2020-12-30 06:01

I\'m iterating over a very large set of strings, which iterates over a smaller set of strings. Due to the size, this method takes a while to do, so to speed it up, I\'m tryi

相关标签:
2条回答
  • 2020-12-30 06:20

    use 'arr.shift'

    a=[1,2,3,4]
    while(a.length!=0)
      print a
      a.shift
      print "\n"
    end
    

    Output:

    [1, 2, 3, 4]
    [2, 3, 4]
    [3, 4]
    [4]

    0 讨论(0)
  • 2020-12-30 06:21

    use delete_if

    array.delete_if do |v|
        if v.should_be_deleted?
            true
        else
            v.update
            false
        end
    end
    
    0 讨论(0)
提交回复
热议问题