Question
How to Skip an Iteration in Ruby .each: Using next Instead of continue
Question
In Ruby, how can you skip the current iteration inside an .each loop, similar to continue in other programming languages?
Short Answer
By the end of this page, you will understand how Ruby skips the current iteration of a loop using next, especially inside .each blocks. You will also see how next compares with break, redo, and loop control in other languages, along with practical examples and common mistakes.
Concept
In many programming languages, continue means: stop the current loop iteration and move to the next one.
Ruby does not use the keyword continue. Instead, Ruby uses next for this purpose.
When you are looping with .each, next immediately ends the current block execution and moves to the next element.
Example:
[1, 2, 3, 4].each do |number|
next if number.even?
puts number
end
Output:
1
3
Here is what happens:
1is not even, so it gets printed.2is even, sonextskips the rest of that iteration.3is printed.4is skipped.
Mental Model
Think of an .each loop like a teacher calling students one by one.
nextmeans: skip this student and call the next onebreakmeans: stop calling students entirely- normal execution means: process this student as usual
So if Ruby is in the middle of handling one item and you say next, Ruby drops the rest of that item's instructions and moves on immediately.
Syntax and Examples
The basic syntax is:
collection.each do |item|
next if condition
# code that runs only when condition is false
end
Example 1: Skip even numbers
numbers = [1, 2, 3, 4, 5]
numbers.each do |number|
next if number.even?
puts number
end
Output:
1
3
5
next if number.even? means:
- if the number is even, skip the rest of the block
- otherwise, continue executing the block
Example 2: Skip blank names
names = ["Alice", "", "Bob", nil, "Carol"]
names.each do |name|
name.? || name.empty?
puts
Step by Step Execution
Consider this example:
numbers = [1, 2, 3]
numbers.each do |n|
puts "Start #{n}"
next if n == 2
puts "End #{n}"
end
Output:
Start 1
End 1
Start 2
Start 3
End 3
Step by step:
numberscontains[1, 2, 3].- Ruby starts
.eachand takes the first value,1. - It prints
Start 1. n == 2is false, sonextdoes not run.- It prints
End 1.
Real World Use Cases
next is useful whenever some items in a collection should not be processed.
Filtering invalid data
rows.each do |row|
next if row[:email].nil? || row[:email].empty?
send_welcome_email(row[:email])
end
Ignoring admin-only restricted actions
users.each do |user|
next unless user.admin?
puts "Granting access to #{user.name}"
end
Skipping failed API responses
responses.each do |response|
next unless response[:status] == 200
process_data(response[:body])
end
Ignoring comments or blank lines in a file
File.readlines().each ||
line = line.strip
line.empty? || line.start_with?()
puts
Real Codebase Usage
In real projects, developers often use next as a guard clause inside loops.
1. Early skip for invalid input
records.each do |record|
next unless record.valid?
save_record(record)
end
This keeps the main logic less nested.
2. Permission checks
tasks.each do |task|
next unless current_user.can_edit?(task)
update_task(task)
end
3. Skipping expensive work
images.each do |image|
next if image.processed?
generate_thumbnail(image)
end
4. Combining with logging
items.each do |item|
if item.nil?
warn "Skipping nil item"
next
end
process(item)
end
Common Mistakes
Mistake 1: Using continue in Ruby
Broken code:
[1, 2, 3].each do |n|
continue if n == 2
puts n
end
Ruby does not have continue.
Correct code:
[1, 2, 3].each do |n|
next if n == 2
puts n
end
Mistake 2: Confusing next with break
Broken idea:
[1, 2, 3, 4].each do |n|
break if n == 2
puts n
end
Output:
Comparisons
| Concept | Ruby keyword/method | What it does |
|---|---|---|
| Skip current iteration | next | Stops the current iteration and moves to the next one |
| Stop the loop entirely | break | Ends the whole loop immediately |
| Repeat current iteration | redo | Restarts the same iteration without moving forward |
| Just transform each item | map | Returns a new array with one output per input |
| Filter items | select / reject | Keeps or removes items based on a condition |
next vs break
Cheat Sheet
# Skip current iteration
next
# Skip current iteration when condition is true
next if condition
# Skip unless condition is true
next unless condition
Common patterns
items.each do |item|
next if item.nil?
process(item)
end
items.each do |item|
next unless item.valid?
process(item)
end
Loop control keywords
next→ skip current iterationbreak→ stop the loopredo→ repeat current iteration
Important rule
Ruby uses next, not continue.
With map
FAQ
What is the Ruby equivalent of continue?
Ruby uses next instead of continue.
Can I use next inside .each?
Yes. next works inside .each blocks to skip the current iteration.
What is the difference between next and break in Ruby?
next skips one iteration. break stops the entire loop.
Does next work only with .each?
No. It also works in other Ruby looping constructs and enumerable blocks.
Why does map return nil when I use next?
Because map expects one output value for each input item. If next has no value, that iteration becomes .
Mini Project
Description
Build a small Ruby script that processes a list of usernames and skips invalid entries. This demonstrates how next helps ignore bad data while continuing to process the rest of the collection.
Goal
Create a script that prints greetings only for valid usernames.
Requirements
- Store several usernames in an array, including empty strings and
nilvalues. - Loop through the array with
.each. - Skip invalid usernames using
next. - Print a greeting for each valid username.
Keep learning
Related questions
Calling a Class Method from an Instance in Ruby
Learn how to call a class method from an instance in Ruby using self.class, with examples, pitfalls, and practical usage patterns.
Calling an Overridden Monkey-Patched Method in Ruby
Learn how to call the original method when monkey patching in Ruby, including alias_method patterns, examples, pitfalls, and practical usage.
Convert a Unix Timestamp to Ruby DateTime
Learn how to convert Unix timestamps to Ruby DateTime and Time objects, with examples, differences, pitfalls, and practical Ruby usage.