Flow control

From Reia

(Redirected from Case)
Jump to: navigation, search

Contents

Branching

if and unless statements

The simplest branching construct available is the if statement, which takes the specified branch if the condition holds true:

if condition
  ...
end

The else keyword specifies an action to be taken if the condition doesn't match:

if condition
  ...
else
  ...
end

Multiple if statements can be chained with the elseif keyword:

if condition1
  ...
elseif condition2
  ...
end

The unless keyword functions like the if keyword, except expression matching is inverted and the branch is taken if the condition does not hold.

unless condition
  ...
end

Branch statements can be used inline by appending them to the end of statements:

do_this() if condition
dont_do_this() unless condition

case statement

case allows you to match several possible conditions against a variable:

case condition
when 42
  ...
when :something
  ...
end

The case statement uses pattern matching for each value of the when clause. Patterns must be indented one level beyond the case declaration, and end with a ':' token.

To match all patterns, the special pattern "_" may be used:

case condition
when :foo
  ...
when :bar
  ...
when :baz
  ...
when _ # catch-all clause
  ...
end

Loops

Sorry, loops are presently unimplemented

The while loop executes so long as the specified condition holds true:

while condition
  ...
end

The until loop executes so long as the condition is false:

until condition
  ...
end

The while and until statements can be placed at the end of the loop as well by using the do keyword:

do
  ...
while condition

For loops can iterate lists:

for x in mylist
  ...
end

This works well when using ranges to generate the list:

for x in 1..10
  ...
end
Personal tools