Ruby for Keyword
last modified October 18, 2026
This tutorial explains how to use Ruby's for keyword for iteration.
The for loop provides a clean syntax for traversing collections.
The for keyword creates a loop that iterates over a collection. It automatically assigns each element to a loop variable. The loop runs until all elements are processed.
While Rubyists often prefer each, for remains familiar
to developers coming from other languages. Unlike a block passed to
each, for does not create a new scope: the loop
variable is an ordinary local variable in the surrounding scope and remains
available after the loop.
Basic for Loop with Range
This simple example demonstrates iterating over a numeric range. The loop variable takes each value in sequence.
for i in 1..5
puts "Current number: #{i}"
end
puts "Final value: #{i}"
The loop runs from 1 to 5 inclusive. Because for does not introduce
a new scope, i is still accessible after the loop; block-scoped
iterators such as each behave differently.
Iterating Over an Array
Arrays work naturally with for loops. Each element becomes
available through the loop variable in turn.
fruits = ["apple", "banana", "cherry"]
for fruit in fruits
puts "Fruit: #{fruit.capitalize}"
end
puts "Last fruit: #{fruit}"
The loop processes each array element sequentially. The variable fruit
holds each value. Again, it persists after the loop completes.
Nested for Loops
for loops can be nested to handle multi-dimensional data. Each
level gets its own loop variable.
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
for row in matrix
for cell in row
print "#{cell} "
end
puts
end
The outer loop handles each row, while the inner loop processes individual cells. This pattern works for any nested enumerable structure.
for with Hash
A hash yields each key-value pair as a two-element array. With two loop variables, Ruby destructures the pair automatically.
person = {name: "John", age: 30, city: "New York"}
for key, value in person
puts "#{key}: #{value}"
end
The loop unpacks each key-value pair into key and
value. With a single loop variable, each item would be a
two-element array such as [:name, "John"].
Modifying Elements in a for Loop
A for loop yields each element rather than a copy of it, but
assigning to the loop variable only rebinds that variable. To change the
collection, mutate the element or assign through its index.
numbers = [1, 2, 3, 4, 5] for num in numbers num *= 2 end puts numbers.inspect
num *= 2 creates a new integer and assigns it to num;
it does not change the array. Since integers are immutable, there is no element
to mutate in place. Use map! to replace the elements, or iterate
over indices and assign to numbers[i].
Break and Next in for Loops
Control flow keywords work similarly in for as in other loops.
break exits entirely, while next skips ahead.
for i in 1..10
next if i % 2 == 0
break if i > 7
puts "Odd number: #{i}"
end
next skips every even number. break ends the loop at the
first odd number greater than 7, which is 9, so the last value printed is 7.
for vs each
This example contrasts for with Ruby's preferred each
iterator. The main differences involve variable scope and return values.
# for loop version
for x in [1, 2, 3]
puts x
end
puts "x after for: #{x}"
# each version
[1, 2, 3].each do |y|
puts y
end
puts "y after each: #{y}" rescue puts "y undefined"
The for variable remains in the surrounding scope. The block
variable y is local to the block, so referencing it afterward
raises a NameError, which the rescue modifier catches.
A for expression also returns the collection it iterated over.
Source
This tutorial covered Ruby's for keyword with examples showing
range iteration, array processing, nested loops, and control flow.
Author
List all Ruby tutorials.