Ruby – The “until” loop

The until-loop is basically the reverse of the until-loop. The syntax of the until loop, here’s an example:

x = 0
until x == 10
  puts x
  x+=1
end

This outputs:

PS C:\Temp\irb> ruby .\until.rb
0
1
2
3
4
5
6
7
8
9
PS C:\Temp\irb>

As before, you can squeeze this until-loop into a single line, like this:

x = 0
until x == 10 do puts x ; x+=1 end

Finally we have the do-until loop, which is equivalent to the do-while loop that we saw earlier:

x = 11
begin
  puts x
  x+=1
end until x >= 10

This outputs:

PS C:\Temp\irb> ruby .\until.rb
11
PS C:\Temp\irb>