https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112018-02-05T15:38:45ZRuby Issue Tracking SystemRuby master - Feature #14444: MatchData: alias for #[]https://bugs.ruby-lang.org/issues/14444?journal_id=701862018-02-05T15:38:45Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><blockquote>
<p>After introduction of safe navigation operator and other latest features,<br>
it could be pretty idiomatic to do this</p>
</blockquote>
<p>This of course assumes that the safe navigation operator is "idiomatic". ;-)</p>
<p>I guess one can say that every possible feature of ruby is "idiomatic", but<br>
I feel that certain ruby style fits less well together than other particular<br>
styles in ruby. Anyway, I am just nitpicking, I understand what you want to<br>
convey (I think).</p>
<blockquote>
<p>Yes, several &. in a row could be said a code smell and example may<br>
feel a bit extreme.</p>
</blockquote>
<p>I don't know if we can state this because one could also say that "&."<br>
may be supoptimal, such as in any lines that already contain other<br>
use of "&", such as .map(&:strip). I am fine with the latter, but the<br>
lonely person & preceding and staring at a . is a bit ... weird to my<br>
eyes. But it depends a lot on how something is used. I still prefer<br>
the { 'key' => 'notation' } for Hashes, but I also make heavy use of<br>
the { key: :notation }, even though the latter is just "symbols" - it<br>
simply is a lot shorter for long hashes, and I like being able to be<br>
succinct. (I also have no problem with Symbols at all, by the way.)</p>
<p>Code like this:</p>
<blockquote>
<p>next_page = url.match(/&page=(\d+)/)&.at(1)</p>
</blockquote>
<p>also seems strange to my eyes.</p>
<p>I don't mind your feature request at all, mind you. If I understood it<br>
correctly then you wish to be able to replace e. g.</p>
<pre><code>&.[](1)
</code></pre>
<p>with a named method such as:</p>
<pre><code>&.capture(1)
</code></pre>
<p>So if this is the sugestion then I am in no way against it, independent<br>
of the "&" there. My reason is primarily because while I myself love<br>
method calls such as [] and variable "names" such as _, and while I<br>
use [] a LOT in my own code as an alternative to "Foo.new" aka "Foo[]",<br>
having a named method to invoke is in my opinion good and makes sense.</p>
<p>We'd get the best of two worlds here - a general, specific name and a<br>
non-"name" such as []. So we can choose what we prefer, which is fine.</p>
<p>I don't have any particular opinion on the name itself, be it .capture()<br>
or .at() or any other name. I think good names are very important but<br>
the even more important thing is whether the general suggestion is ok<br>
or not; and if it is ok, then I guess we can easily find a name.</p>
<p>So in short, I think the alias suggestion in general for [] is ok.</p>
<p>I think that the & is not the prettiest character though.</p> Ruby master - Feature #14444: MatchData: alias for #[]https://bugs.ruby-lang.org/issues/14444?journal_id=701902018-02-05T16:09:29Zzverok (Victor Shepelev)zverok.offline@gmail.com
<ul></ul><p>Idiomaticity of <code>&.</code> is from the same point of view as <code>yield_self</code>'s (<a href="http://zverok.github.io/blog/2018-01-24-yield_self.html" class="external">here</a> is my small blog post about the latter and why it is idiomatic): It allows to express data processing flow in a single chain of methods, instead of local variables, <code>if</code>s and so on.</p>
<p>I tend to use it only for the last step of some chain, which could eventually drop <code>nil</code>. For me subjectively, this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">foo</span><span class="p">.</span><span class="nf">bar</span><span class="p">.</span><span class="nf">baz</span><span class="p">.</span><span class="nf">qwak</span><span class="o">&</span><span class="p">.</span><span class="nf">length</span>
</code></pre>
<p>... Is way more "Ruby idiomatic" than this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">qwak</span> <span class="o">=</span> <span class="n">foo</span><span class="p">.</span><span class="nf">bar</span><span class="p">.</span><span class="nf">baz</span><span class="p">.</span><span class="nf">qwak</span>
<span class="n">qwak</span><span class="p">.</span><span class="nf">length</span> <span class="k">if</span> <span class="n">qwak</span>
<span class="c1"># or</span>
<span class="n">qwak</span> <span class="o">&&</span> <span class="n">qwak</span><span class="p">.</span><span class="nf">length</span>
<span class="c1"># or</span>
<span class="k">if</span> <span class="p">(</span><span class="n">qwak</span> <span class="o">=</span> <span class="n">foo</span><span class="p">.</span><span class="nf">bar</span><span class="p">.</span><span class="nf">baz</span><span class="p">.</span><span class="nf">qwak</span><span class="p">)</span>
<span class="n">qwak</span><span class="p">.</span><span class="nf">length</span>
<span class="k">end</span>
</code></pre> Ruby master - Feature #14444: MatchData: alias for #[]https://bugs.ruby-lang.org/issues/14444?journal_id=761712019-01-10T04:29:14Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li></ul><p>Following is better</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">next_page</span> <span class="o">=</span> <span class="n">response</span><span class="p">.</span><span class="nf">dig</span><span class="p">(</span><span class="s1">'meta'</span><span class="p">,</span> <span class="s1">'pagination'</span><span class="p">,</span> <span class="s1">'next'</span><span class="p">)</span><span class="o">&</span><span class="p">.</span><span class="nf">slice</span><span class="p">(</span><span class="sr">/&page=(\d+)/</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
</code></pre> Ruby master - Feature #14444: MatchData: alias for #[]https://bugs.ruby-lang.org/issues/14444?journal_id=762062019-01-10T10:14:59Zzverok (Victor Shepelev)zverok.offline@gmail.com
<ul></ul><p>Ugh. I feel ashamed, honestly :)<br>
Never thought about using <code>.slice</code> this way, somehow! It is definitely cleaner.<br>
Can be closed.</p> Ruby master - Feature #14444: MatchData: alias for #[]https://bugs.ruby-lang.org/issues/14444?journal_id=941432021-10-15T18:49:32Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Status</strong> changed from <i>Feedback</i> to <i>Closed</i></li></ul>