Equivalent of “continue” in Ruby

后端 未结 7 1338
离开以前
离开以前 2020-12-07 07:13

In C and many other languages, there is a continue keyword that, when used inside of a loop, jumps to the next iteration of the loop. Is there any equivalent of

7条回答
  •  囚心锁ツ
    2020-12-07 07:44

    Writing Ian Purton's answer in a slightly more idiomatic way:

    (1..5).each do |x|
      next if x < 2
      puts x
    end
    

    Prints:

      2
      3
      4
      5
    

提交回复
热议问题