Ruby next Keyword
last modified April 27, 2025
This tutorial explains how to use Ruby's next
keyword. This flow
control statement skips to the next iteration in loops and blocks.
The next keyword immediately jumps to the next iteration of a loop. It skips any remaining code in the current iteration. In blocks, it returns control to the yielding method.
next
is useful for filtering elements or skipping invalid data. It
can optionally take a value to return from the block. Multiple conditions can
trigger different skip behaviors.
Basic next in a Loop
This simple example demonstrates skipping even numbers in a loop. The
next
statement jumps to the next iteration when triggered.
(1..10).each do |i| next if i.even? puts "Processing odd number: #{i}" end
The loop processes numbers 1 through 10. When i.even?
is true,
next
skips the puts
statement. Only odd numbers are
printed.
next with a Value in a Block
next
can return a value from a block to the yielding method. This
example shows how to transform numbers while skipping some.
result = (1..5).map do |n| next n * 10 if n.odd? n end puts result.inspect # [10, 2, 30, 4, 50]
Odd numbers are multiplied by 10 and returned via next
. Even
numbers fall through to return their original value. The result combines both
cases.
Skipping Nested Loops
next
only affects the innermost loop. This example shows nested
loops with conditional skipping.
(1..3).each do |i| puts "Outer loop: #{i}" ('a'..'c').each do |j| next if i == 2 && j == 'b' puts " Inner loop: #{j}" end end
When i
is 2 and j
is 'b', next
skips
only the inner loop's puts. The outer loop continues unaffected.
next in while Loop
next
works in while
loops similarly to iterators.
This example skips processing when a condition is met.
count = 0 while count < 5 count += 1 next if count == 3 puts "Count is #{count}" end
The loop increments count
each iteration. When count
equals 3, next
skips the puts
statement. All other
values are printed.
Conditional next with Multiple Clauses
Multiple conditions can trigger next
with different behaviors.
This example shows complex filtering logic.
(1..20).each do |num| next puts "Skipping #{num}: too small" if num < 5 next puts "Skipping #{num}: multiple of 5" if num % 5 == 0 next if num.even? puts "Processing odd number #{num} (5-20, not multiple of 5)" end
Numbers below 5, multiples of 5, and even numbers are all skipped. Each case has different output behavior. Only qualifying odd numbers are processed.
next in a Method with a Block
When next
is used in a block passed to a method, it returns
control to the method. This example demonstrates the behavior.
def process_items yield(1) puts "After first yield" yield(2) puts "After second yield" yield(3) end process_items do |i| next if i == 2 puts "Processing #{i}" end
The output shows that when next
is triggered, control returns to
the method. The "Processing 2" message is skipped, but method execution
continues.
next vs return in Blocks
This example contrasts next
and return
in blocks.
next
exits just the block, while return
exits the
containing method.
def test_next [1, 2, 3].each do |i| next if i == 2 puts "next: #{i}" end puts "After next block" end def test_return [1, 2, 3].each do |i| return if i == 2 puts "return: #{i}" end puts "This won't print" end test_next test_return
next
skips only iteration 2, then continues. return
exits the entire method at iteration 2. The final message only appears in
test_next
.
Source
This tutorial covered Ruby's next keyword with examples showing loop control, block behavior, and comparisons with other flow control statements.
Author
List all Ruby tutorials.