while executes its body as long as its condition is truthy.
while some_condition do_this end
The condition is first tested and, if truthy, the body is executed. That is, the body might never be executed.
while's type is always
Similar to an
if, if a
while's condition is a variable, the variable is guaranteed to not be
nil inside the body. If the condition is an
var is guaranteed to be of type
Type inside the body. And if the condition is a
var is guaranteed to be of a type that responds to that method.
The type of a variable after a
while depends on the type it had before the
while and the type it had before leaving the
a = 1 while some_condition # a : Int32 | String a = "hello" # a : String a.size end # a : Int32 | String
If you need to execute the body at least once and then check for a breaking condition, you can do this:
while true do_something break if some_condition end
loop, found in the standard library:
loop do do_something break if some_condition end
To the extent possible under law, the persons who contributed to this workhave waived
all copyright and related or neighboring rights to this workby associating CC0 with it.