https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112018-03-23T09:22:47ZRuby Issue Tracking SystemRuby master - Feature #14624: #{nil} allocates a fresh empty string each timehttps://bugs.ruby-lang.org/issues/14624?journal_id=711722018-03-23T09:22:47ZHanmac (Hans Mackowiak)hanmac@gmx.de
<ul></ul><p>i have a problem with that, what if someone might do:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">x</span> <span class="o">=</span> <span class="n">obj</span><span class="p">.</span><span class="nf">to_s</span>
<span class="n">x</span> <span class="o"><<</span> <span class="n">obj2</span><span class="p">.</span><span class="nf">to_s</span>
</code></pre>
<p>then it might be <code>nil</code>, return a frozen string and it would do a <code>RuntimeError</code></p>
<p>better imo would be that if <code>"#{nil}"</code> would do a check if <code>to_s</code> is overwritten or redefined and if not do other magic</p> Ruby master - Feature #14624: #{nil} allocates a fresh empty string each timehttps://bugs.ruby-lang.org/issues/14624?journal_id=712372018-03-27T02:50:08Zbumblingbear (Dillon Welch)
<ul></ul><p>That sounds fine to me!</p>
<p>Hanmac (Hans Mackowiak) wrote:</p>
<blockquote>
<p>i have a problem with that, what if someone might do:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">x</span> <span class="o">=</span> <span class="n">obj</span><span class="p">.</span><span class="nf">to_s</span>
<span class="n">x</span> <span class="o"><<</span> <span class="n">obj2</span><span class="p">.</span><span class="nf">to_s</span>
</code></pre>
<p>then it might be nil, return a frozen string and it would do a <code>RuntimeError</code></p>
<p>better imo would be that if <code>"#{nil}"</code> would do a check if <code>to_s</code> is overwritten or redefined and if not do other magic</p>
</blockquote> Ruby master - Feature #14624: #{nil} allocates a fresh empty string each timehttps://bugs.ruby-lang.org/issues/14624?journal_id=712382018-03-27T04:05:31Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>I'm confused; are you proposing that <code>nil.to_s</code> returns the same String object every time, or that string interpolation detects a <code>nil</code> object and optimises it?</p>
<p>A lot of times I rely on <code>"#{foo}"</code> returning a new String object that contains a copy of the #to_s of <code>foo</code>, so it seems to me like spec that <code>"#{nil}"</code> returns a new empty string every time.</p> Ruby master - Feature #14624: #{nil} allocates a fresh empty string each timehttps://bugs.ruby-lang.org/issues/14624?journal_id=712392018-03-27T04:17:12Zbumblingbear (Dillon Welch)
<ul></ul><p>Closer to the string interpolation optimization part. The problem with <code>"#{nil}"</code> is that it allocates two empty strings each time. I feel like it would be possible to do this with zero/one allocation. I don't know what the underlying code looks like, but the logic I'm thinking of is something like</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">interpolate</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
<span class="k">if</span> <span class="n">obj</span> <span class="o">==</span> <span class="kp">nil</span>
<span class="s1">''</span>
<span class="k">else</span>
<span class="n">obj</span><span class="p">.</span><span class="nf">to_s</span> <span class="c1"># assuming this is current behavior</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>phluid61 (Matthew Kerwin) wrote:</p>
<blockquote>
<p>I'm confused; are you proposing that <code>nil.to_s</code> returns the same String object every time, or that string interpolation detects a <code>nil</code> object and optimises it?</p>
<p>A lot of times I rely on <code>"#{foo}"</code> returning a new String object that contains a copy of the #to_s of <code>foo</code>, so it seems to me like spec that <code>"#{nil}"</code> returns a new empty string every time.</p>
</blockquote> Ruby master - Feature #14624: #{nil} allocates a fresh empty string each timehttps://bugs.ruby-lang.org/issues/14624?journal_id=712462018-03-27T08:34:53Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Tracker</strong> changed from <i>Bug</i> to <i>Feature</i></li><li><strong>Description</strong> updated (<a title="View differences" href="/journals/71246/diff?detail_id=48796">diff</a>)</li><li><strong>Backport</strong> deleted (<del><i>2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN</i></del>)</li></ul> Ruby master - Feature #14624: #{nil} allocates a fresh empty string each timehttps://bugs.ruby-lang.org/issues/14624?journal_id=713512018-03-31T15:43:27Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p><a href="https://github.com/nobu/ruby/tree/feature/opt_to_s" class="external">https://github.com/nobu/ruby/tree/feature/opt_to_s</a></p> Ruby master - Feature #14624: #{nil} allocates a fresh empty string each timehttps://bugs.ruby-lang.org/issues/14624?journal_id=715952018-04-21T11:12:46Znormalperson (Eric Wong)normalperson@yhbt.net
<ul></ul><p><a href="mailto:nobu@ruby-lang.org" class="email">nobu@ruby-lang.org</a> wrote:</p>
<blockquote>
<p><a href="https://github.com/nobu/ruby/tree/feature/opt_to_s" class="external">https://github.com/nobu/ruby/tree/feature/opt_to_s</a></p>
</blockquote>
<p>Btw, I also had <a href="/issues/13715">[ruby-core:81905]</a> [Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: [PATCH] avoid garbage from Symbol#to_s in interpolation (Closed)" href="https://bugs.ruby-lang.org/issues/13715">#13715</a>] from last<br>
year but forgot about it :x</p>