switch Statement
The switch statement selects a block of code to execute based on the value of an expression.
switch expression do
case value1:
# body
case value2, value3:
# body
default:
# body
end
Syntax Rules
- The
dokeyword follows the switch expression - Multiple values per case are comma-separated
- The colon after case values is optional
- The
defaultclause is optional and handles unmatched values - The block is closed with
end
No Fall-Through
Only the matched case executes. There is no fall-through between cases, and no break is needed.
switch Data.action do
case "create":
Data.status = "created"
case "update", "patch":
Data.status = "updated"
default:
Data.status = "unknown"
end
In this example, if Data.action is "update" or "patch", only the second case body executes. The default case is skipped.