https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112012-12-10T21:52:11ZRuby Issue Tracking SystemRuby master - Bug #7539: Misleading error message "can't convert nil into string"https://bugs.ruby-lang.org/issues/7539?journal_id=345862012-12-10T21:52:11Zkosaki (Motohiro KOSAKI)kosaki.motohiro@gmail.com
<ul></ul><p>I support "can't append nil to string" error.</p> Ruby master - Bug #7539: Misleading error message "can't convert nil into string"https://bugs.ruby-lang.org/issues/7539?journal_id=345902012-12-11T00:44:58Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>You'll get this error message for every failed conversion; the use the corresponding implicit conversion methods (<code>to_str</code>, <code>to_int</code>, ...)</p>
<pre><code>"" + 42
[].concat(1..2)
</code></pre>
<p>I'm surprised this leads to confusion, but we could change the message to something like:</p>
<pre><code>"no implicit conversion of nil into String"
</code></pre> Ruby master - Bug #7539: Misleading error message "can't convert nil into string"https://bugs.ruby-lang.org/issues/7539?journal_id=345912012-12-11T01:44:20Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>kosaki (Motohiro KOSAKI) wrote:</p>
<blockquote>
<p>I support "can't append nil to string" error.</p>
</blockquote>
<p>I'd prefer a more descriptive message like:</p>
<p>"can't append nil to string (object doesn't respond to <code>to_str</code>)"</p>
<p>This way someone would know that this would work:</p>
<blockquote>
<p>nil.class.module_eval{alias to_str to_s}; '' + nil</p>
</blockquote> Ruby master - Bug #7539: Misleading error message "can't convert nil into string"https://bugs.ruby-lang.org/issues/7539?journal_id=345942012-12-11T06:23:14Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>I support adding the 'implicit' word to the error message.</p>
<blockquote>
<pre><code>"no implicit conversion of nil into String"
</code></pre>
</blockquote>
<p>I remember when I first encountered it, knowing to search for "implicit<br>
conversion" would have helped me understand the difference between #to_s<br>
and #to_str earlier.</p>
<p>[note: it's more than just implementing #to_str , there are other<br>
implications for implicit casting]</p> Ruby master - Bug #7539: Misleading error message "can't convert nil into string"https://bugs.ruby-lang.org/issues/7539?journal_id=345982012-12-11T13:49:34Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul><li><strong>File</strong> <a href="/attachments/3339">patch#7539.txt</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/3339/patch%237539.txt">patch#7539.txt</a> added</li><li><strong>Target version</strong> changed from <i>1.9.3</i> to <i>2.6</i></li></ul><p>rosenfeld (Rodrigo Rosenfeld Rosas) wrote:</p>
<blockquote>
<p>kosaki (Motohiro KOSAKI) wrote:</p>
<blockquote>
<p>I support "can't append nil to string" error.</p>
</blockquote>
<p>I'd prefer a more descriptive message like:</p>
<p>"can't append nil to string (object doesn't respond to <code>to_str</code>)"</p>
</blockquote>
<p>Looking at the implementation, a "can't append" would need some special code. A message such as ""can't convert nil into String (using to_str)" would be relatively easy to generate. I have attached a patch (not compiled or tested, sorry). This change would show up in many other places that use the same conversion logic, so let's make sure we really want it.</p> Ruby master - Bug #7539: Misleading error message "can't convert nil into string"https://bugs.ruby-lang.org/issues/7539?journal_id=345992012-12-11T14:17:17Zdrbrain (Eric Hodel)drbrain@segment7.net
<ul></ul><p>I am worried about implying that the user should add to_str, to_ary, etc. to fix this problem. Rarely is implementing these methods appropriate since they are used for objects that are intended to behave exactly as a String, Array, etc.</p>
<p>An error message that omitted mention of these methods can help prevent users from accidentally shooting themselves in the foot.</p> Ruby master - Bug #7539: Misleading error message "can't convert nil into string"https://bugs.ruby-lang.org/issues/7539?journal_id=346142012-12-11T23:52:41Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Assignee</strong> set to <i>marcandre (Marc-Andre Lafortune)</i></li></ul><blockquote>
<p>Looking at the implementation, a "can't append" would need some special code</p>
</blockquote>
<p>Indeed. It would be redundant anyways, since the error message starts with "in `+':", or whichever method was called last.</p>
<blockquote>
<p>I am worried about implying that the user should add to_str, to_ary, etc. to fix this problem</p>
</blockquote>
<p>Agreed, which is why I proposed making explicit the fact that implicit conversion failed.</p>
<p>So, I'll commit "no implicit conversion of into " unless there's a better wording proposed</p> Ruby master - Bug #7539: Misleading error message "can't convert nil into string"https://bugs.ruby-lang.org/issues/7539?journal_id=357092013-01-30T07:01:01Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li><li><strong>% Done</strong> changed from <i>0</i> to <i>100</i></li></ul><p>This issue was solved with changeset r38979.<br>
Chris, thank you for reporting this issue.<br>
Your contribution to Ruby is greatly appreciated.<br>
May Ruby be with you.</p>
<hr>
<ul>
<li>
<p>object.c: Improve error for failed implicit conversions [Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Misleading error message "can't convert nil into string" (Closed)" href="https://bugs.ruby-lang.org/issues/7539">#7539</a>]</p>
</li>
<li>
<p>error.c: Adapt rdoc</p>
</li>
<li>
<p>test/ruby/test_object.rb: Test for above</p>
</li>
</ul>