Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this not a syntax error?

Tags:

syntax

ruby

If I do this:

(false true)

it fails with a syntax error, as I expect. But if I do this:

(false
 true)

the code is executed, and it throws away the first condition and returns the result of the second.

Is this considered a bug or a feature?

like image 897
L2G Avatar asked Dec 07 '11 19:12

L2G


1 Answers

Line endings are optional, so in this case, the return is causing the parser to interpret it as the following:

(false; true)

which evaluates to just:

(true)

If these were method calls then both would be evaluated, but only the last would be emitted. For example:

x = (p "hello"
p "world"
2)

This will output "hello" and "world" and x will equal 2

like image 75
J. Holmes Avatar answered Oct 01 '22 18:10

J. Holmes