https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112015-04-05T02:28:40ZRuby Issue Tracking SystemRuby master - Feature #11034: Nil Conditionalhttps://redmine.ruby-lang.org/issues/11034?journal_id=520412015-04-05T02:28:40Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/52041/diff?detail_id=37504">diff</a>)</li></ul><p>A local variable written in code always exists.<br>
I think this is a duplicate ticket but can't find the former tickets.</p> Ruby master - Feature #11034: Nil Conditionalhttps://redmine.ruby-lang.org/issues/11034?journal_id=520422015-04-05T02:32:58Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>IIRC, there were some discussions like:</p>
<ul>
<li>the condition is only whether <code>nil</code> or not, or whether responds to the method or not</li>
<li>the result when the condition doesn't match will be the receiver itself or <code>nil</code>
</li>
</ul>
<p>and others.</p>
<p>Also, I'm afraid that <code>??</code> would conflict with the current syntax.</p> Ruby master - Feature #11034: Nil Conditionalhttps://redmine.ruby-lang.org/issues/11034?journal_id=520432015-04-05T05:55:33Zgrzesiek (Grzegorz Bizon)grzesiek.bizon@gmail.com
<ul></ul><p>Hi Nobuyoshi, thanks for reply !</p>
<p>Yes, this Nil Conditional Operator has to introduce new syntax. That is why this cannot be achieved via gem. Currently - of course - double question mark <em>??</em> violates syntax (in some cases) or is treated as tenary operator. But this doesn't have to be <em>??</em>.</p>
<p>I also agree that my version of nil conditional is too permissive and shouldn't return nil if variable does not exist. This was just less difficult to implement ;)</p>
<p>I simply believe that Nil Conditional Operator would be useful for every rubyist. In my opinion it is worth talking over, considering.</p>
<p>I will try to find related issue.</p>
<p>Kind regards,<br>
Grzegorz</p> Ruby master - Feature #11034: Nil Conditionalhttps://redmine.ruby-lang.org/issues/11034?journal_id=520442015-04-05T08:40:40Zrecursive-madman (Recursive Madman)recursive.madman@gmx.de
<ul></ul><p>This is similar to a pattern I have seen somewhere else:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">var</span> <span class="o">=</span> <span class="mf">123.4</span>
<span class="n">var</span><span class="p">.</span><span class="nf">try</span><span class="p">.</span><span class="nf">round</span> <span class="c1">#=> 123</span>
<span class="n">var</span> <span class="o">=</span> <span class="kp">nil</span>
<span class="n">var</span><span class="p">.</span><span class="nf">try</span><span class="p">.</span><span class="nf">round</span> <span class="c1">#=> nil</span>
</code></pre>
<p>Which doesn't introduce a new operator and can be implemented like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Object</span>
<span class="k">def</span> <span class="nf">try</span>
<span class="nb">self</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">NilClass</span>
<span class="k">def</span> <span class="nf">try</span>
<span class="no">IgnoreCall</span><span class="p">.</span><span class="nf">new</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">IgnoreCall</span>
<span class="k">def</span> <span class="nf">method_missing</span><span class="p">(</span><span class="o">*</span><span class="n">a</span><span class="p">)</span>
<span class="kp">nil</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>By the way, fun fact:</p>
<pre><code>?????:?? #=> '?'
</code></pre> Ruby master - Feature #11034: Nil Conditionalhttps://redmine.ruby-lang.org/issues/11034?journal_id=520532015-04-06T19:06:07Zgrzesiek (Grzegorz Bizon)grzesiek.bizon@gmail.com
<ul></ul><p>@Recursive Madman - <em>try</em> is not native function. It is Active Support Core Extension.</p>
<p>Moreover implementation and behavior of try is not stable (is changing depending on Rails version), it will not work when creating train wrecks and is terribly ugly.</p>
<p>Of course - this can be, in some specific way, achieved by using gem (like <em>nil_conditional</em> gem), but, in my opinion, only native implementation will be reasonable enough. <strong>Only native solution for that problem will be stable, consistent, short and concise.</strong></p>
<p>What do you think ?</p>
<p>Kind regards,<br>
Grzegorz</p> Ruby master - Feature #11034: Nil Conditionalhttps://redmine.ruby-lang.org/issues/11034?journal_id=520922015-04-10T04:34:59Zakr (Akira Tanaka)akr@fsij.org
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-6 priority-4 priority-default closed" href="/issues/1122">Feature #1122</a>: request for: Object#try</i> added</li></ul> Ruby master - Feature #11034: Nil Conditionalhttps://redmine.ruby-lang.org/issues/11034?journal_id=521552015-04-14T06:51:16Zgrzesiek (Grzegorz Bizon)grzesiek.bizon@gmail.com
<ul></ul><p>Hi again !</p>
<p>I've made new attempt to create nil conditional operator (<a href="https://github.com/nil-conditional" class="external">https://github.com/nil-conditional</a>). This is Ruby implementation, you can check it out here: <a href="https://github.com/grzesiek/nil-conditional/blob/master/lib/nil_conditional.rb" class="external">https://github.com/grzesiek/nil-conditional/blob/master/lib/nil_conditional.rb</a>.</p>
<p>It is simple, it encapsulates objects in NilConditional instance. Sample usage:</p>
<pre><code>def nil_test(str)
str._?.concat("456")
end
nil_test("123")
=> "123456"
nil_test(nil)
=> #<NilConditional:0x007fc602016718 @object=nil>
nil_test(nil).nil?
=> true
</code></pre>
<p>What do you think about that ? It seems (for me) that this is sufficient, and native implementation is not necessary with this approach.</p>
<p>Anyway, thanks for feedback !</p>
<p>Kind regards,<br>
Grzegorz</p> Ruby master - Feature #11034: Nil Conditionalhttps://redmine.ruby-lang.org/issues/11034?journal_id=542342015-09-18T09:58:49Zakr (Akira Tanaka)akr@fsij.org
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-5 priority-4 priority-default closed" href="/issues/11537">Feature #11537</a>: Introduce "Safe navigation operator"</i> added</li></ul> Ruby master - Feature #11034: Nil Conditionalhttps://redmine.ruby-lang.org/issues/11034?journal_id=545022015-10-21T08:06:45Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>We will introduce <code>.?</code> <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Introduce "Safe navigation operator" (Closed)" href="https://redmine.ruby-lang.org/issues/11537">#11537</a> which address this proposal.</p>
<p>Matz.</p>