https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112017-12-27T14:05:14ZRuby Issue Tracking SystemRuby master - Feature #14249: Remove str[match_str]https://redmine.ruby-lang.org/issues/14249?journal_id=690442017-12-27T14:05:14Zana06 (Ana Maria Martinez Gomez)
<ul><li><strong>Tracker</strong> changed from <i>Bug</i> to <i>Feature</i></li><li><strong>ruby -v</strong> deleted (<del><i>2.4</i></del>)</li><li><strong>Backport</strong> deleted (<del><i>2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN</i></del>)</li></ul> Ruby master - Feature #14249: Remove str[match_str]https://redmine.ruby-lang.org/issues/14249?journal_id=690462017-12-27T16:12:21Znaruse (Yui NARUSE)naruse@airemix.jp
<ul></ul><p>Use Hash#dig instead.</p> Ruby master - Feature #14249: Remove str[match_str]https://redmine.ruby-lang.org/issues/14249?journal_id=719542018-05-11T09:57:47Zana06 (Ana Maria Martinez Gomez)
<ul></ul><p>My example in the first comment is not really correct (something went wrong when copying). What I meant:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">params</span> <span class="o">=</span> <span class="p">{</span> <span class="s2">"user"</span> <span class="o">=></span> <span class="p">{</span> <span class="s2">"name"</span> <span class="o">=></span> <span class="s2">"Nicolas Cage"</span><span class="p">,</span> <span class="s2">"age"</span> <span class="o">=></span> <span class="mi">27</span> <span class="p">}</span> <span class="p">}</span>
<span class="n">params</span><span class="p">[</span><span class="s2">"user"</span><span class="p">][</span><span class="s2">"age"</span><span class="p">]</span> <span class="c1">#=> 27</span>
<span class="n">params</span> <span class="o">=</span> <span class="p">{</span> <span class="s2">"user"</span> <span class="o">=></span> <span class="s2">"Nicolas Cage"</span> <span class="p">}</span>
<span class="n">params</span><span class="p">[</span><span class="s2">"user"</span><span class="p">][</span><span class="s2">"age"</span><span class="p">]</span> <span class="c1">#=> "age"</span>
</code></pre>
<p><a class="user active user-mention" href="https://redmine.ruby-lang.org/users/5">@naruse (Yui NARUSE)</a> using dig works nicely for this case, but then the question is if it makes sense to keep <code>str[match_str]</code>. It is a confusing method...</p> Ruby master - Feature #14249: Remove str[match_str]https://redmine.ruby-lang.org/issues/14249?journal_id=719582018-05-11T10:20:11ZHanmac (Hans Mackowiak)hanmac@gmx.de
<ul></ul><p>I can tell you that the <code>str[match_str]</code> is used in many codes, but i can't find any example now</p>
<p>and no, it is different from <code>str[regex]</code> !</p> Ruby master - Feature #14249: Remove str[match_str]https://redmine.ruby-lang.org/issues/14249?journal_id=719782018-05-13T23:32:57Zyugui (Yuki Sonoda)yugui@yugui.jp
<ul></ul><p>IIUC, Ana is saying that <code>str[match_str]</code> does not make sense because it always returns <code>match_str</code>.<br>
But it does make sense because it returns <code>nil</code> if <code>str</code> does not contain such a substring. i.e. it is useful to test if the string contains a substring.</p>
<p>You would say that <code>str[regex]</code> can do the same thing. And theoretically it is true. But it also means you must compile <code>match_str</code> into <code>Regexp</code> beforehand.<br>
So <code>str[match_str]</code> is a convenient (and sometimes more efficient) way to test substrings.</p>
<p>Then, you might say that <code>str.contains</code> can do the same thing. And theoretically it is true. Here <code>str[match_str]</code> is necessary to just keep compatibility between <code>String</code> and <code>Regexp</code>.<br>
Since most of all methods in the standard library which expect a pattern of character sequence accepts both of <code>String</code> and <code>Regexp</code>, it is natural for users to expect that the compatibility is kept in <code>String#[]</code> too.</p> Ruby master - Feature #14249: Remove str[match_str]https://redmine.ruby-lang.org/issues/14249?journal_id=719922018-05-14T10:57:02ZEregon (Benoit Daloze)
<ul></ul><p>I believe "abc".include?("ab") is much clearer than "abc"["ab"].<br>
Are there real usages of it?<br>
I guess the main concern here is compatibility.</p> Ruby master - Feature #14249: Remove str[match_str]https://redmine.ruby-lang.org/issues/14249?journal_id=719932018-05-14T13:54:22Zgotoken (Kentaro Goto)gotoken@gmail.com
<ul></ul><p>A use in replacement may look intuitive.</p>
<pre><code>s = "Cage"
s["age"] = "heese"
p s
#=> "Cheese"
</code></pre> Ruby master - Feature #14249: Remove str[match_str]https://redmine.ruby-lang.org/issues/14249?journal_id=721842018-05-19T18:13:35Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>As Kentaro showed, there are use cases for [], as-in replacement.</p>
<p>It is quite concise so I think it may be useful for some ruby hackers.</p>
<p>I personally use the more verbose variants usually; as Benoit showed,<br>
".include?" checks, and then often .tr() or .sub() or .gsub(). It<br>
is not as concise as Kentaro's example, but I like working with<br>
.sub() and .gsub() and .include?() so I tend to use them when possible.</p>
<p>I also use Regexes a lot; they are extremely useful.</p>
<p>Of course in the above, a regex can be used and I think there is more<br>
ruby code that uses regexes instead:</p>
<pre><code>s[/age/] = "heese"
</code></pre>
<p>Mostly because, I think, because it will be more flexible, since you<br>
can check for more substring-replacements, and it sort of combines<br>
the .include? check as well, since the replacement is only done when<br>
there is a match to the regex. Regexes are absolutely awesome in ruby.</p>
<p>I don't know offhand about differences as hanmac stated, but I personally<br>
use regexes almost always, so the // syntax. But Yuki also mentioned a<br>
difference, e. g. the String having to become a regex (and I remember<br>
in the old pickaxe, that creating a regex is more costly than creating<br>
a String).</p>
<p>Since some time we can use regexes for .start_with?() and .end_with?()<br>
checks; before that, I think we could use only Strings.</p>
<p>In .include?() checks, we seem to be only able to use Strings. Would<br>
adding regex support there be good? I don't want to distract from the<br>
issue here, so I am just mentioning it.</p>
<p>Anyway, Ana wrote:</p>
<blockquote>
<p>the question is if it makes sense to keep str[match_str]. It is a<br>
confusing method...</p>
</blockquote>
<p>I don't really think it is a confusing method - it seems very simple<br>
to me. I don't use it myself because of alternatives, but I think<br>
trying to want to make ruby "perfect" in every theoretical aspect,<br>
is very, very difficult, even without backwards compatibility.</p>
<p>Ruby is a great language but that does not necessarily mean that 100%<br>
of ruby is perfect (or great). Ruby hackers have to decide on what<br>
they want to write, how to write it and so forth.</p>
<p>What works in my case is to focus (and use) what I like in ruby and<br>
mostly ignore the rest. (I am not speaking about string[match_string]<br>
but e. g. @@class_variables as example; I realized one day that I do<br>
not need it, so I stopped using them and everything is fine).</p>
<p>Perhaps that also works for your use cases when writing ruby code Ana.</p>