Limited Offer
60% OFF on all plans!

Loops in Ruby

Repeat actions in Ruby using loop, while, until, for, times, upto, and the each iterator with break, next, and redo control flow.

Introduction to Loops

Loops repeat a block of code. Ruby offers several loop forms: `loop`, `while`, `until`, `for`, `times`, `upto`, `downto`, and `each`. The `each` iterator is the most idiomatic.

1. Loop Types at a Glance

Ruby's loop options, from low-level to idiomatic.

times Loop

`n.times` runs a block exactly `n` times. The block receives the current index (0-based).

1. Basic times

Call `times` on any integer to repeat a block that many times.

2. times Without Index

Omit the block variable when you just need to repeat.

while Loop

`while condition` keeps running the block as long as the condition is truthy. Check the condition before each iteration.

1. Basic while

Use `while` when you do not know the number of iterations in advance.

2. begin...end while (do-while equivalent)

Run the block at least once, then check the condition.

until Loop

`until condition` is the opposite of `while` — it loops as long as the condition is false.

1. Basic until

`until` reads more naturally for negative conditions.

2. Inline until

Place `until` at the end of a statement for a modifier loop.

loop — Infinite Loop

`loop` creates an infinite loop. Use `break` with a condition to exit.

1. loop with break

`loop` runs forever until `break` is reached.

2. loop with break Value

`break value` makes the `loop` expression return that value.

for Loop

Ruby has a `for...in` loop, but `each` is preferred. `for` does not create a new scope — the loop variable persists after the loop.

1. for with Range

Iterate over a range of numbers using `for`.

each — Idiomatic Ruby Iteration

`each` is the Ruby-idiomatic way to iterate over any collection. It creates a proper block scope and works on arrays, hashes, ranges, and more.

1. each on an Array

Iterate over every element of an array.

2. each_with_index

Get both the element and its index.

3. each on a Hash

Iterate over key-value pairs of a hash.

4. each on a Range

Use `each` on a Range to iterate over a sequence of numbers or characters.

upto, downto, and step

`upto` and `downto` are called on integers to iterate between two values. `step` iterates with a custom increment.

1. upto, downto, and step

`1.upto(5)` counts up; `5.downto(1)` counts down; `step` uses a custom increment.

break & next

`break` exits a loop immediately. `next` skips the rest of the current iteration and moves to the next. Ruby uses `next` instead of `continue`.

1. break — Exit the Loop

`break` stops the loop immediately.

2. next — Skip an Iteration

`next` skips the current iteration and jumps to the next.

Reverse Loops

Use `reverse_each`, `downto`, or `reverse` on an array to iterate from the end.

1. reverse_each

`reverse_each` iterates an array from last element to first without creating a new array.

2. downto for Numbers

Count down with `downto`.

Best Practices

Prefer Ruby's iterators over low-level loops. Use `each`, `map`, `select`, and `reduce` for expressive, readable iteration.

1. Prefer Iterators Over while

Ruby's enumerable methods are more expressive and less error-prone than manual loops.

2. Guard Clauses in Loops

Use `next` at the top of a loop to skip invalid items early.