ZetCode

Ruby break Keyword

last modified October 18, 2026

This tutorial explains how to use Ruby's break keyword to control loop execution. The break statement provides early termination of loops.

The break keyword immediately exits the current loop or iterator block. In a loop, execution continues with the statement after the loop. When a block passed to a method calls break, the method call returns immediately.

Break is useful for stopping iteration when a condition is met or an error occurs. It works with Ruby's loops and with iterators such as each and times.

Basic break in while Loop

This example shows break terminating a while loop when a counter reaches 5. The loop would normally run 10 times but exits early.

basic_while_break.rb
counter = 0

while counter < 10
  puts counter
  break if counter == 5
  counter += 1
end

puts "Loop ended"

The loop prints the numbers 0 through 5, then breaks. Without the break, it would continue to 9. The final message confirms that execution continues after the loop.

break in each Iterator

Break works similarly in iterators like each. This example searches an array for a specific value and breaks when found.

array_search.rb
fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']

fruits.each do |fruit|
  puts "Checking #{fruit}"
  if fruit == 'date'
    puts "Found the date!"
    break
  end
end

puts "Search complete"

The loop checks each fruit until it finds 'date'. The break stops iteration before 'elderberry' is checked, and execution continues after the block.

break with Value

Ruby's break can return a value to the loop's context. This value becomes the result of the loop expression.

break_with_value.rb
result = loop do
  num = rand(1..100)
  puts "Generated: #{num}"
  break num if num > 90
end

puts "High number: #{result}"

The loop generates random numbers until one exceeds 90. The break returns this number, which is assigned to result. This pattern is useful for searches.

Nested Loops with break

Break only exits the innermost loop. This example demonstrates breaking from a nested loop structure.

nested_loops.rb
(1..3).each do |i|
  puts "Outer loop: #{i}"
  
  (1..3).each do |j|
    puts "  Inner loop: #{j}"
    break if j == 2
  end
end

The inner loop breaks when j reaches 2, but the outer loop continues. Each outer iteration starts a new inner loop, and break affects only its immediate enclosing loop.

break in case Statement

A case statement is not a loop, so break inside it exits the nearest enclosing loop or block. Without such an enclosing construct, break is a syntax error. This example shows conditional breaking.

case_with_break.rb
count = 0

loop do
  count += 1
  case count
  when 1..3
    puts "Counting #{count}"
  when 4
    puts "Stopping at 4"
    break
  else
    break
  end
end

The case statement handles different count ranges. When count reaches 4, it prints a message and breaks the loop. The else clause is a fallback, but this example never reaches it.

break vs return

Break exits loops, while return exits methods. This example contrasts their behavior in similar contexts.

break_vs_return.rb
def test_break
  [1, 2, 3].each do |n|
    puts n
    break if n == 2
  end
  puts "After break"
end

def test_return
  [1, 2, 3].each do |n|
    puts n
    return if n == 2
  end
  puts "After return"
end

test_break
test_return

Break exits just the loop, so "After break" prints. Return exits the entire method, skipping "After return". Choose based on desired scope of exit.

break in Custom Methods

A break in a block passed to a method ends the method call. This example shows a custom method that yields to a block.

custom_method.rb
def process_items
  items = [10, 20, 30, 40, 50]
  items.each do |item|
    yield(item)
  end
  :normal_exit
end

result = process_items do |n|
  puts "Processing #{n}"
  break :early_exit if n == 30
end

puts "Result: #{result}"

The block breaks when it hits 30, so process_items returns :early_exit. Without the break, it would return :normal_exit after processing all items.

Source

Ruby Keywords Documentation

This tutorial covered Ruby's break keyword with examples showing loop control, return values, and method interactions. Use break when a loop should stop as soon as a condition is met.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Ruby tutorials.