Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple lines within parentheses

Tags:

ruby

The following multi-line conditional statement returns unexpected result.

if (false and
false and
false
true)
  puts 123
end
# => 123

Notice the missing condition. Wondering why ruby interpreter did not detect the syntax problem in the condition.

like image 674
ftaher Avatar asked Sep 18 '25 11:09

ftaher


1 Answers

There is no syntax error there.

The newline character started a new expression, exactly the same way semicolon (;) does it.

(false and false and false; true)
# => true

This operator is like the comma operator found in C and C++.

a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value

...and similar to a do-form in Clojure:

Evaluates the expressions in order and returns the value of the last.

like image 110
D-side Avatar answered Sep 21 '25 08:09

D-side