Ruby: while, until, for, each, unless, case, break, next, redo, retry

# if/else; Note: elsif
if then [else] end
if then [elsif] end

# Executes code while conditional is true.
while conditional [do]
code
end

# Executes code while conditional is true. (at least once)
begin
code
end while conditional

# Executes code while conditional is false.
until conditional [do]
code
end

# Executes code while conditional is false. (at least once)
begin
code
end until conditional

# Executes code once for each element in expression.
for variable [, variable ...] in expression [do]
code
end

for i in 0..5
puts "Value of local variable is #{i}"
end

(expression).each do |variable[, variable...]| code end

case expression
[when expression [, expression ...] [then]
code ]...
[else
code ]
end

# Executes code if conditional is true.
code if conditional

# Executes code if conditional is false.
code unless conditional

break

next    # Jumps to next iteration (like i+1)

redo    # Restarts this iteration (like repeat i)

retry   # restart from beginning (like, i=0)

No comments:

Post a Comment