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


shevegen (Robert A. Heiler) wrote:
> I am slightly against this proposal.
> 
> To me personally it does not make a lot of sense to differ between the
> trailing character of a method in regards to the ternary operator. IMO
> the ternary operator should not discriminate against last characters
> of any method, ever. It would also add a bit more complexity and I
> am not sure if the trade-off is worth to be had here.
> 
> Another problem I see is the prolific use of '?'. It would be better, 
> in my opinion, to keep the syntax simple(r), if possible. With many
> special syntax and combinations of syntax in use, it becomes harder
> to determine what is going on in general.
> 
> Ultimately you only have to convince matz about the use case and
> proposed benefit of a suggestion, though, not me. :)
> 
> (I should also add that I use the trailing token '?' for methods a
> LOT,  but I avoid using the ternary operator, largely because I have 
> found that it takes me longer to determine what is going on,
> as opposed to e. g. if/else checks, even though they require more
> lines of code.)

While I'm not going to argue that `if else end` statements read nicer than the ternary statement. Reality is that the ternary terms to be the preferred option by many rubyist for one-liners. Case in point: [ https://github.com/rubocop-hq/ruby-style-guide ] prefers the ternary operator)

There is a strong precedent of ruby making an exceptional case for the case of readability. Case in point: Rocket Hash syntax vs modern hash syntax

My reasoning for why the exception is guaranteed is that
1- It is best practice to use the ternary operator on actual true/false rather than truthy, falsey statements
2- It is best practice in ruby to define methods returning booleans with a trailing `?`
3- For many projects, it is best practice to use ternary operators on one liners.

The corollary is that things like
`variable.nil? ? 'something' : 'something else'`
Happen a lot

And I think it doesn't read as nice. Which is one thing we tend to value.

I definitely understand the point against it (having an exception on the ternary operator)

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

* 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>