Question
In C and many other programming languages, the continue keyword is used inside a loop to skip the rest of the current iteration and move directly to the next one. Is there an equivalent to continue in Ruby?
Short Answer
By the end of this page, you will understand that Ruby uses next as the equivalent of continue. You will learn how next behaves in different kinds of loops, how it helps skip unwanted work, and how to use it clearly in real Ruby code.
Concept
In Ruby, the equivalent of continue is next.
When Ruby reaches next inside a loop or block iteration, it stops executing the rest of the current iteration and immediately moves to the next one.
This matters because loops often process many items, and sometimes you want to skip certain values:
- ignore invalid input
- skip empty strings
- avoid unnecessary work
- handle special cases early
Ruby does not use the keyword continue for this purpose. Instead, it uses a small set of loop-control keywords:
next→ skip to the next iterationbreak→ leave the loop completelyredo→ repeat the current iteration without moving on
A simple 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 is printed2is even, so skips the
Mental Model
Think of a loop like a teacher calling students one by one.
For each student, the teacher normally does the full routine:
- call the name
- ask a question
- record the answer
But sometimes the student is absent. In that case, the teacher says, “skip this one, go to the next student.”
That is what next does in Ruby.
next= skip this turn and move onbreak= stop calling students entirelyredo= repeat this same student's turn again
So if continue in another language means “skip the rest of this loop cycle,” Ruby's next means the same idea.
Syntax and Examples
Basic syntax
next
It is usually used inside a loop or iterator block:
array.each do |item|
next if some_condition
# code here runs only when the condition is false
end
Example 1: Skip even numbers
(1..5).each do |number|
next if number.even?
puts number
end
Output:
1
3
5
Explanation:
next if number.even?skips2and4- only odd numbers reach
puts
Example 2: Skip blank strings
Step by Step Execution
Consider this code:
numbers = [1, 2, 3, 4]
numbers.each do |n|
next if n > 2
puts "Processing #{n}"
end
Step-by-step trace
Iteration 1
nis1n > 2isfalsenextdoes not runputs "Processing 1"runs
Iteration 2
nis2n > 2isfalsenextdoes not runputs "Processing 2"runs
Iteration 3
Real World Use Cases
next is useful whenever some items should be ignored during loop processing.
1. Skipping invalid records
users.each do |user|
next unless user[:email]
send_email(user)
end
Only users with an email address are processed.
2. Ignoring comment or empty lines in files
File.readlines("data.txt").each do |line|
line = line.strip
next if line.empty? || line.start_with?("#")
puts "Processing: #{line}"
end
Useful in configuration files, import scripts, and CLI tools.
3. Skipping failed API items
responses.each do |response|
next unless response[:success]
save_data(response[:data])
end
This avoids running code on bad responses.
4. Filtering data during iteration
Real Codebase Usage
In real Ruby codebases, next is often used to keep loops flatter and easier to read.
Guard-style skipping
Developers often skip invalid cases early:
rows.each do |row|
next if row.nil?
next if row[:status] != "active"
process(row)
end
This avoids deeply nested if statements.
Validation before processing
params.each do |key, value|
next if value.nil?
config[key] = value
end
Only valid values are copied.
Working with enumerables
Ruby developers often combine next with methods like each, map, and select.
slugs = titles.map ||
title.? || title.empty?
title.downcase.gsub(, )
Common Mistakes
1. Using continue instead of next
Ruby does not have a continue keyword.
Broken code:
[1, 2, 3].each do |n|
continue if n == 2
puts n
end
Correct code:
[1, 2, 3].each do |n|
next if n == 2
puts n
end
2. Confusing next with break
nextskips one iterationbreakstops the loop entirely
[1, 2, 3, 4].each ||
n ==
puts n
Comparisons
| Concept | What it does | Ruby keyword/method | When to use |
|---|---|---|---|
| Skip current iteration | Ignore the rest of this loop cycle and move on | next | When one item should be skipped |
| Stop loop entirely | Exit the loop now | break | When no more iterations are needed |
| Repeat same iteration | Start the current iteration again | redo | Rare cases where the same item must be retried |
| Filter collection | Keep only matching items | select, reject | When building a filtered result is the real goal |
next vs
Cheat Sheet
# Ruby equivalent of continue
next
Common patterns
next if condition
next unless condition
Example with each
items.each do |item|
next if item.nil?
puts item
end
Example with while
i = 0
while i < 5
i += 1
next if i == 3
puts i
end
Related loop control keywords
break # exit the loop completely
next # skip to the next iteration
redo # repeat the current iteration
FAQ
Is there a continue keyword in Ruby?
No. Ruby uses next instead of continue.
What is the difference between next and break in Ruby?
next skips the current iteration and continues the loop. break exits the loop completely.
Can I use next inside each blocks in Ruby?
Yes. That is one of the most common places to use it.
Can next be used in a while loop?
Yes. It works in while, until, and iterator blocks. Be careful to update loop variables correctly.
Does next return a value in Ruby?
In iterator methods like map, next can provide the value for that block iteration.
When should I use instead of ?
Mini Project
Description
Build a small Ruby script that processes a list of usernames and skips invalid entries. This demonstrates how next helps you ignore bad data while continuing to handle the rest of the list.
Goal
Create a script that prints only valid usernames and skips blank or blocked entries using next.
Requirements
- Create an array of usernames that includes valid names, empty strings, and blocked names.
- Loop through the usernames with Ruby iteration.
- Use
nextto skip empty usernames. - Use
nextto skip blocked usernames. - Print a greeting for each valid username.
Keep learning
Related questions
How to Call Shell Commands from Ruby and Capture Output
Learn how to run shell commands in Ruby, capture output, check exit status, and choose the right method for scripts and apps.
How to Check Whether a String Contains a Substring in Ruby
Learn how to check if a string contains a substring in Ruby using include?, match, and multiline string examples.
How to Check if a Hash Key Exists in Ruby
Learn how to check whether a specific key exists in a Ruby hash using key?, has_key?, and include? with clear examples.