Skip to content

Control Flow

if condition
# ...
elsif other_condition
# ...
else
# ...
end

if accepts only these types as a condition:

  • Bool — true or false
  • Bool? — falsy when nil or false, truthy when true
  • Any? — falsy when nil, truthy otherwise (nil check)

Any other type is a compile error. This keeps truthiness predictable.

active: Bool = true
if 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 branch
end
flag: Bool? = lookup_flag
if flag # ok — Bool? (falsy when nil or false)
if "hello" # compile error — String is not Bool or Any?

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?
end
unless invalid?
save
end

Equivalent to if !condition. Accepts the same types as if.

return if invalid?
log message unless quiet?
save! if dirty?

if/unless return a value — the last expression in the taken branch:

status = if active? then "active" else "inactive" end
label = unless muted? then "loud" else "quiet" end
label = active? ? "active" : "inactive"

case value
when :active then "active"
when :pending then "pending"
when String then "a string: #{value}"
when 1, 2, 3 then "one, two, or three"
else "unknown"
end

case/when is an expression — it returns the value of the matched branch.

when supports three forms of matching:

  • Value match — when :active, when 1, 2, 3. Compared using ==. Multiple values in one when are 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 using Range#includes?.
case shape
when Circle then Math::PI * shape.radius ** 2 # shape is Circle here
when Rectangle then shape.width * shape.height # shape is Rectangle here
end

There 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 condition
# ...
end
until condition
# ...
end

Infinite loop — use break to exit:

loop do
input = read_input
break if input == "quit"
end

Iterates over a collection:

for item in collection
puts item
end
for char in "hello"
puts char
end

Classic init/condition/increment loop:

for i = 0; i < 10; i += 1
puts i
end

The loop variable is scoped to the loop. Type is inferred from the initialiser.


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.


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 / or are low-precedence control-flow alternatives to && / ||. Prefer && / || in expressions; use and / or only for flow control:
    save! or raise "failed" # idiomatic use of or
  • not is a low-precedence alternative to !. Prefer ! in expressions.
  • Postfix if / unless bind lower than all of the above.