The syntax of a conditional expression in Ruby is:
if c_1 then e_1 elsif c_2 then e_2 elsif c_3 then e_3 … elsif c_n then e_n else e_nplus1 end
where c_1 … c_n and e_1 … e_nplus1 can be arbitrary Ruby expressions.
It is possible to use an expression separator (i.e. ; or newline) instead of the then keyword to separate the parts of the conditional expression.
With semicolon (this usage is non-idiomatic):
if c_1; e_1 elsif c_2; e_2 elsif c_3; e_3 … elsif c_n; e_n else e_nplus1 end
With newlines:
if c_1
e_1
elsif c_2
e_2
elsif c_3
e_3
# …
elsif c_n
e_n
else
e_nplus1
end
If you use newlines, you can optionally also use the then keyword, but that is non-idiomatic, too:
if c_1
then e_1
elsif c_2
then e_2
elsif c_3
then e_3
# …
elsif c_n
then e_n
else
e_nplus1
end
So, in your case, the correct syntax would be:
# idiomatic
a.each { |i| if i % 3 == 0 then puts "three" elsif i % 5 == 0 then puts "five" else puts i end }
# non-idiomatic
a.each { |i| if i % 3 == 0; puts "three" elsif i % 5 == 0; puts "five" else puts i end }
# idiomatic
a.each { |i|
if i % 3 == 0
puts "three"
elsif i % 5 == 0
puts "five"
else
puts i
end
}
# non-idiomatic
a.each { |i|
if i % 3 == 0
then puts "three"
elsif i % 5 == 0
then puts "five"
else
puts i
end
}
However, for such a chain of if / elsif, it is typically more idiomatic to use a case expression:
# idiomatic
case when c_1 then e_1 when c_2 then e_2 when c_3 then e_3 … when c_n then e_n else e_nplus1 end
# non-idiomatic
case when c_1; e_1 when c_2; e_2 when c_3; e_3 … when c_n; e_n else e_nplus1 end
# idiomatic
case
when c_1
e_1
when c_2
e_2
when c_3
e_3
# …
when c_n
e_n
else
e_nplus1
end
# non-idiomatic
case
when c_1
then e_1
when c_2
then e_2
when c_3
then e_3
# …
when c_n
then e_n
else
e_nplus1
end
Which in your case would look like this:
# idiomatic
a.each { |i| case when i % 3 == 0 then puts "three" when i % 5 == 0 then puts "five" else puts i end }
# non-idiomatic
a.each { |i| case when i % 3 == 0; puts "three" when i % 5 == 0; puts "five" else puts i end }
# idiomatic
a.each { |i|
case
when i % 3 == 0
puts "three"
when i % 5 == 0
puts "five"
else
puts i
end
}
# non-idiomatic
a.each { |i|
case
when i % 3 == 0
then puts "three"
when i % 5 == 0
then puts "five"
else
puts i
end
}
Note that the conditional expressions (both if and case) are expressions, not statements. There are no statements in Ruby, everything is an expression, everything evaluates to a value. A conditional expression evaluates to the value of the expression in the branch that was taken.
So, you could also write it like this:
# idiomatic
a.each { |i| puts(if i % 3 == 0 then "three" elsif i % 5 == 0 then "five" else i end) }
# non-idiomatic
a.each { |i| puts(if i % 3 == 0; "three" elsif i % 5 == 0; "five" else i end) }
# idiomatic
a.each { |i|
puts(if i % 3 == 0
"three"
elsif i % 5 == 0
"five"
else
i
end)
}
# non-idiomatic
a.each { |i|
puts(if i % 3 == 0
then "three"
elsif i % 5 == 0
then "five"
else
i
end)
}
# idiomatic
a.each { |i| puts(case when i % 3 == 0 then "three" when i % 5 == 0 then "five" else i end) }
# non-idiomatic
a.each { |i| puts(case when i % 3 == 0; "three" when i % 5 == 0; "five" else i end) }
# idiomatic
a.each { |i|
puts(case
when i % 3 == 0
"three"
when i % 5 == 0
"five"
else
i
end)
}
# non-idiomatic
a.each { |i|
puts(case
when i % 3 == 0
then "three"
when i % 5 == 0
then "five"
else
i
end)
}