Feature #12929
closed
ternary should look ahead w/in a block (and not care about newlines)
Added by ag4ve (shawn wilson) over 7 years ago.
Updated over 2 years ago.
Description
foo = (bar ? 0 : 1) # works
foo = (bar ?
0 : 1) # works
foo = (bar
? 0
: 1) # doesn't work
Files
In Ruby, all operators have to be written at the end of the line to make it possible to distinguish between complete statements (with a missing semicolon) and continuations.
The only exception is the '.' operator, which is allowed to be on the next line to make method chaining look better, as follows:
array.map ...
.select ...
.reject ...
.group ...
.sort ...
What do you think is special about the ternary operator?
Martin Dürst wrote:
In Ruby, all operators have to be written at the end of the line to make it possible to distinguish between complete statements (with a missing semicolon) and continuations.
The only exception is the '.' operator, which is allowed to be on the next line to make method chaining look better, as follows:
array.map ...
.select ...
.reject ...
.group ...
.sort ...
What do you think is special about the ternary operator?
I think that this is what he meant. If he uses IRB, then the syntax error comes up like:
SyntaxError: (irb):14: syntax error, unexpected '?', expecting ')'
from /Users/antonionesic/.rvm/rubies/ruby-2.3.1/bin/irb:11:in `<main>'
But I have to agree. This is a SyntaxError, and not a bug.
Ticket should be closed.
- Tracker changed from Bug to Feature
- Description updated (diff)
- Status changed from Open to Assigned
- Assignee set to matz (Yukihiro Matsumoto)
It is not a bug, and easy to extend the parser to allow that syntax, but doesn't look nice to me.
I don't think it's worth modifying the syntax. If you want to write complex conditional code, use if-statement.
Matz.
- Status changed from Assigned to Rejected
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like0