Ruby Language Reference

until statement

Syntax

until (condition is false)
  p 'do something'
end

Executes the loop when condition is false.

Example

i = 0
until i == 3
  p "The value of i is : #{i}"
  i += 1
end

The conditional is evaluated before the code within the until-end block is executed. This prints:

The value of i is : 0
The value of i is : 1
The value of i is : 2

Then the program terminates.

Until Statement Modifier Form

i = 0

p "The value of i is : #{i}. Incrementing to #{i += 1}" until i == 3

This prints:

The value of i is : 0. Incrementing to 1
The value of i is : 1. Incrementing to 2
The value of i is : 2. Incrementing to 3

Then the program terminates.

Until Modifier with Begin Statement

i = 0

begin
  p "The value of i is : #{i}"
  i += 1   
end until i == 3

This prints:

The value of i is : 0
The value of i is : 1
The value of i is : 2

Then the program terminates. The code within the begin-end block is executed once before the conditional is evaluated.


Sharing is Caring