call next on ruby loop from external method

前端 未结 3 1609
孤城傲影
孤城傲影 2021-01-11 14:16

in Ruby it\'s easy to tell loop to go to next item

(1..10).each do |a|
  next if a.even?
  puts a
end

result =>

1
3   
5
         


        
3条回答
  •  走了就别回头了
    2021-01-11 14:30

    Enumerator#next and Enumerator#peek will be good option to goo :

    def my_complex_method(e)
      return if e.peek.even? 
      p e.peek
    end
    enum = (1..5).each
    enum.size.times do |a|
      my_complex_method(enum)
      enum.next
    end
    

    Output

    1
    3
    5
    

提交回复
热议问题