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?!
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.
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'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With