Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"OR" Operator must be placed at end of previous line? (unexpected tOROP)

I am running Ruby 1.9.

This is a valid syntax:

items = (data['DELETE'] || data['delete'] ||
         data['GET'] || data['get'] || data['POST'] || data['post'])

But this gives me an error:

items = (data['DELETE'] || data['delete']
         || data['GET'] || data['get'] || data['POST'] || data['post'])

t.rb:8: syntax error, unexpected tOROP, expecting ')'
         || data['GET'] || data['get'] |...
           ^

Why?!

like image 764
akonsu Avatar asked Nov 21 '12 21:11

akonsu


Video Answer


2 Answers

What I can say is "that's just how it works".

The Ruby parser does an amazing job, in general, of figuring out when an expression needs to continue on another line. Just about every other language in the world completely punts on this problem and requires an actual character to either continue to the next line or terminate the statement.

As you know, Ruby is special in that, almost always, it just figures it out.

In this case, though, there is a conflict. The parser knows that your expression isn't finished, because it's still looking for the ), but it could be a compound expression.

For example, you could be writing something like this:

(p :a; p :b; p :c)

...but using the newline soft terminator instead of ; ... this syntax below does actually work:

(p :a
 p :b
 p :c)

(BTW, the value of that expression is the value of the last expression in the sequence.)

Ruby can't parse both your statement and the above one without a better hint such as a binary operator that clearly requires another line.

like image 158
DigitalRoss Avatar answered Oct 27 '22 00:10

DigitalRoss


Ruby interprets end of line as the end of statement. Operators indicate continuation of a statement.

You could use backslash \ to indicate continuation as well so the following will work

items = (data['DELETE'] || data['delete'] \
     || data['GET'] || data['get'] || data['POST'] || data['post'])
like image 39
rohit89 Avatar answered Oct 27 '22 01:10

rohit89