Issue #16451 has been updated by myxoh (Nicolas Klein).


nobu (Nobuyoshi Nakada) wrote:
> A method ends with `?` also can take argument(s), so the proposed syntax introduces an ambiguity which doesn't seem solvable.

I won't claim to be an expert in how the interpreter works, so I'll take your word for it. 
I'm not sure why that would be the case, here are some examples as to how I'd expect this to work

```ruby
foo.include?(object) included_statement : none_included_statement
foo.include? object included_statement : none_included_statement
foo.include? included_statement : none_included_statement #=> raises ArgumentError (wrong number of arguments expected 1 given 0)
# Assuming the method bar? has optional arguments
foo.bar? bar_is_true : bar_is_false # foo.bar? is executed with no optional arguments
foo.bar? true bar_is_true : bar_is_false # foo.bar? is executed with `true` as the first optional param
foo.bar? true, nil, third(false) bar_is_true : bar_is_false # foo.bar? is executed with arguments `(true, nil, third(false))`
```

Alternatively, I think a reasonable compromise is to raise an syntax error if the arguments are not surrounded by bracks (like we do if you call `method submethod argument` rather than `method submethod(argument)`

Or even raise a syntax error if the question method uses arguments with this shortened syntax


----------------------------------------
Feature #16451: Special ternary operator for methods ending in `?`
https://bugs.ruby-lang.org/issues/16451#change-83429

* Author: myxoh (Nicolas Klein)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
When a method ends in `?`, we should be able to skip the `?` in the ternary operator. For example, we should be able to do:

```ruby
question_method? true_statement : false_statement
```

This shouldn't interfere with implementations, as it currently fails to parse. Below are examples:

```ruby
def normal_method; true end
def question_method?; false end

question_method? ? 'was true' : 'was false' # => 'was false'
question_method?   'was true' : 'was false' # => 'was false'
normal_method ?    'was true' : 'was false' # => 'was true'
```



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>