As an expression
The value of an if
is the value of the last expression found in each of its branches:
a = if 2 > 1
3
else
4
end
a # => 3
If an if
branch is empty, or it’s missing, it’s considered as if it had nil
in it:
if 1 > 2
3
end
# The above is the same as:
if 1 > 2
3
else
nil
end
# Another example:
if 1 > 2
else
3
end
# The above is the same as:
if 1 > 2
nil
else
3
end
To the extent possible under law, the persons who contributed to this workhave waived
all copyright and related or neighboring rights to this workby associating CC0 with it.
https://crystal-lang.org/reference/syntax_and_semantics/as_an_expression.html