Ruby break Keyword
last modified April 27, 2025
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. Program execution continues with the statement following the loop.
Break is useful for stopping iteration when a condition is met or an error occurs. It works with all loop types and enumerable methods in Ruby.
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.
counter = 0 while counter < 10 puts counter break if counter == 5 counter += 1 end puts "Loop ended"
The loop prints numbers 0 through 5, then breaks. Without break, it would continue to 9. The message after the loop confirms normal termination.
break in each Iterator
Break works similarly in iterators like each. This example searches an array for a specific value and breaks when found.
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'. Break stops further iteration unnecessarily checking 'elderberry'. The search completes immediately.
break with Value
Ruby's break can return a value to the loop's context. This value becomes the result of the loop expression.
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. Break returns this number, which gets 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.
(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 creates a new inner loop instance. Break affects only its immediate containing loop.
break in case Statement
While break is mainly for loops, it can appear in case statements within loops. This example shows conditional breaking.
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 also breaks for safety.
break vs return
Break exits loops, while return exits methods. This example contrasts their behavior in similar contexts.
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
Methods using yield can respond to break from their blocks. This example shows a custom method handling break appropriately.
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, making the method return :early_exit. Without break, it would return :normal_exit after processing all items.
Source
This tutorial covered Ruby's break keyword with examples showing loop control, value returns, and method interactions. Use break to optimize loop execution.
Author
List all Ruby tutorials.