Ruby Language Reference
next statement
Next in While
song = %w(singing in the rain done)
index = 0
while index < song.length
word = song[index]
index += 1
next if word == 'rain'
puts "Currently singing the word: #{word}"
end
This prints:
Currently singing the word: singing
Currently singing the word: in
Currently singing the word: the
Currently singing the word: done
As you can see, the word singing
is skipped.
Next Inside a Method with a Block
def test
yield
end
result = test { next }
p result
This prints nil
. It terminates execution of a block if called within a block.