Control Flow
Conditionals
Section titled “Conditionals”if / elsif / else
Section titled “if / elsif / else”if condition # ...elsif other_condition # ...else # ...endif accepts only these types as a condition:
Bool—trueorfalseBool?— falsy whennilorfalse, truthy whentrueAny?— falsy whennil, truthy otherwise (nil check)
Any other type is a compile error. This keeps truthiness predictable.
active: Bool = trueif active # ok — Bool
user: User? = find_user(id)if user # ok — Any? (nil check) user.name # compiler narrows user to User (non-nullable) inside this branchend
flag: Bool? = lookup_flagif flag # ok — Bool? (falsy when nil or false)
if "hello" # compile error — String is not Bool or Any?Smart Narrowing
Section titled “Smart Narrowing”When an Any? value passes a nil check, the compiler narrows its type to non-nullable
within the truthy branch:
name: String? = maybe_name()
if name puts name.upcase # name is String here, not String?endunless
Section titled “unless”unless invalid? saveendEquivalent to if !condition. Accepts the same types as if.
Postfix Conditionals
Section titled “Postfix Conditionals”return if invalid?log message unless quiet?save! if dirty?As Expressions
Section titled “As Expressions”if/unless return a value — the last expression in the taken branch:
status = if active? then "active" else "inactive" endlabel = unless muted? then "loud" else "quiet" endTernary
Section titled “Ternary”label = active? ? "active" : "inactive"case / when
Section titled “case / when”case valuewhen :active then "active"when :pending then "pending"when String then "a string: #{value}"when 1, 2, 3 then "one, two, or three"else "unknown"endcase/when is an expression — it returns the value of the matched branch.
Matching Rules
Section titled “Matching Rules”when supports three forms of matching:
- Value match —
when :active,when 1, 2, 3. Compared using==. Multiple values in onewhenare an OR — any match takes the branch. - Type match —
when Circle. Checks whether the value is an instance of that type. The compiler narrows the value to that type within the branch. - Range match —
when 1..10. Checks membership usingRange#includes?.
case shapewhen Circle then Math::PI * shape.radius ** 2 # shape is Circle herewhen Rectangle then shape.width * shape.height # shape is Rectangle hereendThere is no === operator — when dispatches using the rules above directly.
Custom types cannot hook into case/when matching beyond implementing ==.
Pattern matching (destructuring in when) is deferred to a future revision.
while / until
Section titled “while / until”while condition # ...end
until condition # ...endInfinite loop — use break to exit:
loop do input = read_input break if input == "quit"endfor / in
Section titled “for / in”Iterates over a collection:
for item in collection puts itemend
for char in "hello" puts charendC-style for
Section titled “C-style for”Classic init/condition/increment loop:
for i = 0; i < 10; i += 1 puts iendThe loop variable is scoped to the loop. Type is inferred from the initialiser.
Loop Control
Section titled “Loop Control”| Keyword | Behaviour |
|---|---|
break |
Exit the loop (or exit the yielding method from a block) |
next |
Skip to the next iteration |
return |
Return from the enclosing method |
retry |
Retry the begin block (in rescue only) |
See Blocks and Closures for break/next/return semantics inside blocks.
Operator Precedence
Section titled “Operator Precedence”Operators listed from highest to lowest precedence. Operators on the same row have equal precedence and associate in the direction shown.
| Precedence | Operators | Associativity |
|---|---|---|
| Highest | !, ~, unary +, unary - |
Right |
** |
Right | |
*, /, % |
Left | |
+, - |
Left | |
<<, >> |
Left | |
& |
Left | |
|, ^ |
Left | |
<, <=, >, >= |
Left | |
<=>, ==, != |
Left | |
&& |
Left | |
|| |
Left | |
.., ... |
Non-associative | |
? : |
Right | |
=, +=, -=, *=, /=, %=, **=, <<=, >>=, &=, |=, ^= |
Right | |
not |
Right | |
| Lowest | or, and |
Left |
Notes:
- Method calls,
., and&.bind tighter than all operators and are not listed in the table. and/orare low-precedence control-flow alternatives to&&/||. Prefer&&/||in expressions; useand/oronly for flow control:save! or raise "failed" # idiomatic use of ornotis a low-precedence alternative to!. Prefer!in expressions.- Postfix
if/unlessbind lower than all of the above.