https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112019-12-25T16:37:24ZRuby Issue Tracking SystemRuby master - Feature #16451: Special ternary operator for methods ending in `?`https://redmine.ruby-lang.org/issues/16451?journal_id=833982019-12-25T16:37:24Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>I am slightly against this proposal.</p>
<p>To me personally it does not make a lot of sense to differ between the<br>
trailing character of a method in regards to the ternary operator. IMO<br>
the ternary operator should not discriminate against last characters<br>
of any method, ever. It would also add a bit more complexity and I<br>
am not sure if the trade-off is worth to be had here.</p>
<p>Another problem I see is the prolific use of '?'. It would be better,<br>
in my opinion, to keep the syntax simple(r), if possible. With many<br>
special syntax and combinations of syntax in use, it becomes harder<br>
to determine what is going on in general.</p>
<p>Ultimately you only have to convince matz about the use case and<br>
proposed benefit of a suggestion, though, not me. :)</p>
<p>(I should also add that I use the trailing token '?' for methods a<br>
LOT, but I avoid using the ternary operator, largely because I have<br>
found that it takes me longer to determine what is going on,<br>
as opposed to e. g. if/else checks, even though they require more<br>
lines of code.)</p> Ruby master - Feature #16451: Special ternary operator for methods ending in `?`https://redmine.ruby-lang.org/issues/16451?journal_id=833992019-12-25T17:13:16Zsawa (Tsuyoshi Sawada)
<ul><li><strong>Subject</strong> changed from <i>Special terniary operators for methods ending in `?`</i> to <i>Special ternary operator for methods ending in `?`</i></li><li><strong>Description</strong> updated (<a title="View differences" href="/journals/83399/diff?detail_id=55926">diff</a>)</li></ul> Ruby master - Feature #16451: Special ternary operator for methods ending in `?`https://redmine.ruby-lang.org/issues/16451?journal_id=834002019-12-25T17:18:36Zsawa (Tsuyoshi Sawada)
<ul></ul><p>The current ternary operator does not work that way. It works like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">question_method?</span> <span class="p">?</span> <span class="s1">'was true'</span> <span class="p">:</span> <span class="s1">'was false'</span> <span class="c1"># => 'was false'</span>
</code></pre> Ruby master - Feature #16451: Special ternary operator for methods ending in `?`https://redmine.ruby-lang.org/issues/16451?journal_id=834042019-12-25T22:25:38Zmyxoh (Nicolas Klein)
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/83404/diff?detail_id=55930">diff</a>)</li></ul> Ruby master - Feature #16451: Special ternary operator for methods ending in `?`https://redmine.ruby-lang.org/issues/16451?journal_id=834052019-12-25T22:26:26Zmyxoh (Nicolas Klein)
<ul></ul><p>sawa (Tsuyoshi Sawada) wrote:</p>
<blockquote>
<p>The current ternary operator does not work that way. It works like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">question_method?</span> <span class="p">?</span> <span class="s1">'was true'</span> <span class="p">:</span> <span class="s1">'was false'</span> <span class="c1"># => 'was false'</span>
</code></pre>
</blockquote>
<p>You are right, I've corrected the example</p> Ruby master - Feature #16451: Special ternary operator for methods ending in `?`https://redmine.ruby-lang.org/issues/16451?journal_id=834062019-12-25T22:36:36Zmyxoh (Nicolas Klein)
<ul></ul><p>shevegen (Robert A. Heiler) wrote:</p>
<blockquote>
<p>I am slightly against this proposal.</p>
<p>To me personally it does not make a lot of sense to differ between the<br>
trailing character of a method in regards to the ternary operator. IMO<br>
the ternary operator should not discriminate against last characters<br>
of any method, ever. It would also add a bit more complexity and I<br>
am not sure if the trade-off is worth to be had here.</p>
<p>Another problem I see is the prolific use of '?'. It would be better,<br>
in my opinion, to keep the syntax simple(r), if possible. With many<br>
special syntax and combinations of syntax in use, it becomes harder<br>
to determine what is going on in general.</p>
<p>Ultimately you only have to convince matz about the use case and<br>
proposed benefit of a suggestion, though, not me. :)</p>
<p>(I should also add that I use the trailing token '?' for methods a<br>
LOT, but I avoid using the ternary operator, largely because I have<br>
found that it takes me longer to determine what is going on,<br>
as opposed to e. g. if/else checks, even though they require more<br>
lines of code.)</p>
</blockquote>
<p>While I'm not going to argue that <code>if else end</code> 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)</p>
<p>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</p>
<p>My reasoning for why the exception is guaranteed is that<br>
1- It is best practice to use the ternary operator on actual true/false rather than truthy, falsey statements<br>
2- It is best practice in ruby to define methods returning booleans with a trailing <code>?</code><br>
3- For many projects, it is best practice to use ternary operators on one liners.</p>
<p>The corollary is that things like<br>
<code>variable.nil? ? 'something' : 'something else'</code><br>
Happen a lot</p>
<p>And I think it doesn't read as nice. Which is one thing we tend to value.</p>
<p>I definitely understand the point against it (having an exception on the ternary operator)</p> Ruby master - Feature #16451: Special ternary operator for methods ending in `?`https://redmine.ruby-lang.org/issues/16451?journal_id=834242019-12-26T08:09:26Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>A method ends with <code>?</code> also can take argument(s), so the proposed syntax introduces an ambiguity which doesn't seem solvable.</p> Ruby master - Feature #16451: Special ternary operator for methods ending in `?`https://redmine.ruby-lang.org/issues/16451?journal_id=834292019-12-26T10:15:42Zmyxoh (Nicolas Klein)
<ul></ul><p>nobu (Nobuyoshi Nakada) wrote:</p>
<blockquote>
<p>A method ends with <code>?</code> also can take argument(s), so the proposed syntax introduces an ambiguity which doesn't seem solvable.</p>
</blockquote>
<p>I won't claim to be an expert in how the interpreter works, so I'll take your word for it.<br>
I'm not sure why that would be the case, here are some examples as to how I'd expect this to work</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">foo</span><span class="p">.</span><span class="nf">include?</span><span class="p">(</span><span class="n">object</span><span class="p">)</span> <span class="n">included_statement</span> <span class="p">:</span> <span class="n">none_included_statement</span>
<span class="n">foo</span><span class="p">.</span><span class="nf">include?</span> <span class="n">object</span> <span class="n">included_statement</span> <span class="p">:</span> <span class="n">none_included_statement</span>
<span class="n">foo</span><span class="p">.</span><span class="nf">include?</span> <span class="n">included_statement</span> <span class="p">:</span> <span class="n">none_included_statement</span> <span class="c1">#=> raises ArgumentError (wrong number of arguments expected 1 given 0)</span>
<span class="c1"># Assuming the method bar? has optional arguments</span>
<span class="n">foo</span><span class="p">.</span><span class="nf">bar?</span> <span class="n">bar_is_true</span> <span class="p">:</span> <span class="n">bar_is_false</span> <span class="c1"># foo.bar? is executed with no optional arguments</span>
<span class="n">foo</span><span class="p">.</span><span class="nf">bar?</span> <span class="kp">true</span> <span class="n">bar_is_true</span> <span class="p">:</span> <span class="n">bar_is_false</span> <span class="c1"># foo.bar? is executed with `true` as the first optional param</span>
<span class="n">foo</span><span class="p">.</span><span class="nf">bar?</span> <span class="kp">true</span><span class="p">,</span> <span class="kp">nil</span><span class="p">,</span> <span class="n">third</span><span class="p">(</span><span class="kp">false</span><span class="p">)</span> <span class="n">bar_is_true</span> <span class="p">:</span> <span class="n">bar_is_false</span> <span class="c1"># foo.bar? is executed with arguments `(true, nil, third(false))`</span>
</code></pre>
<p>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 <code>method submethod argument</code> rather than <code>method submethod(argument)</code></p>
<p>Or even raise a syntax error if the question method uses arguments with this shortened syntax</p> Ruby master - Feature #16451: Special ternary operator for methods ending in `?`https://redmine.ruby-lang.org/issues/16451?journal_id=834302019-12-26T10:32:48ZxBartu (Bartu Demirkıran)
<ul></ul><p>I am also against the proposal.</p>
<p>Having said that I love using one-liners but newcomers to Ruby (from another language) will presumably look for current syntax.</p>
<p>This might be a bit odd yet we can advocate the following:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">(</span><span class="n">question_method?</span><span class="p">)</span> <span class="p">?</span> <span class="s1">'was true'</span> <span class="p">:</span> <span class="s1">'was false'</span>
</code></pre> Ruby master - Feature #16451: Special ternary operator for methods ending in `?`https://redmine.ruby-lang.org/issues/16451?journal_id=834332019-12-26T21:03:48ZDan0042 (Daniel DeLorme)
<ul></ul><p>Also against.</p>
<p>I agree that the double question mark looks weird in <code>foo? ? 1 : 2</code>, but that's just a code formatting issue and doesn't warrant such an exceptional syntax. xBartu's suggestion is good, or you could customize your editor's syntax highlighting to de-emphasize the second question mark.</p>