From: "Kumar R." Date: 2013-03-05T06:58:52+09:00 Subject: How `next` works in ruby with `unless` ? The `next` statement is used to skip a part of the loop and continue with the next iteration of the loop. It can be used in combination with `for` and `while` statements. I have seen people using next if there is complicated piece of code after some condition is being evaluated i.e next if @state!=:some_state # some long complicated code Now here I have played with the `next` in my `IRB` as below : n = 1 loop do n = n + 1 next unless n == 10 print "Good" break end # Good=> nil The above one understood. Nicely clear. n = 1 #=> 1 loop do print "#{n}" n = n + 1 next puts "hi" unless n == 5 p "good" break end #1hi #2hi #3hi #4"good" #=> nil In the above code, couldn't understand about which order the lines `puts "hi"` and `unless n == 5` executed. Which executed first? The below one leads to the `infinite Loop`. n = 1 #=> 1 loop do print "#{n}" n = n + 1 next puts "hi"; 2 + 3 unless n == 5 p "good" break end whereas this one is good: n = 1 #=> 1 loop do print "#{n}" n = n + 1 next puts "hi", 2 + 3 unless n == 5 p "good" break end #1hi #5 #2hi #5 #3hi #5 #4"good" #=> nil Please help me here to understand - how does this one resolve that `forever` loop ? -- Posted via http://www.ruby-forum.com/.