Bug #7539
closed
Misleading error message "can't convert nil into string"
Added by connec (Chris Connelly) about 12 years ago.
Updated almost 12 years ago.
Description
When trying to call String#+
with nil
as an argument, you get the error "can't convert nil into String", which does not make sense (in fact seeming blatantly false) as nil.to_s
, String(nil)
etc. all return ''
without errors.
Ideally, this method should use to_s
to convert the argument, or else report an error along the lines of "can't append nil to string".
Minimal test case:
Actual:
> '' + nil
TypeError: can't convert nil into String
Expected:
> '' + nil
''
Files
I support "can't append nil to string" error.
You'll get this error message for every failed conversion; the use the corresponding implicit conversion methods (to_str
, to_int
, ...)
"" + 42
[].concat(1..2)
I'm surprised this leads to confusion, but we could change the message to something like:
"no implicit conversion of nil into String"
kosaki (Motohiro KOSAKI) wrote:
I support "can't append nil to string" error.
I'd prefer a more descriptive message like:
"can't append nil to string (object doesn't respond to to_str
)"
This way someone would know that this would work:
nil.class.module_eval{alias to_str to_s}; '' + nil
I support adding the 'implicit' word to the error message.
"no implicit conversion of nil into String"
I remember when I first encountered it, knowing to search for "implicit
conversion" would have helped me understand the difference between #to_s
and #to_str earlier.
[note: it's more than just implementing #to_str , there are other
implications for implicit casting]
rosenfeld (Rodrigo Rosenfeld Rosas) wrote:
kosaki (Motohiro KOSAKI) wrote:
I support "can't append nil to string" error.
I'd prefer a more descriptive message like:
"can't append nil to string (object doesn't respond to to_str
)"
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.
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.
An error message that omitted mention of these methods can help prevent users from accidentally shooting themselves in the foot.
- Assignee set to marcandre (Marc-Andre Lafortune)
Looking at the implementation, a "can't append" would need some special code
Indeed. It would be redundant anyways, since the error message starts with "in `+':", or whichever method was called last.
I am worried about implying that the user should add to_str, to_ary, etc. to fix this problem
Agreed, which is why I proposed making explicit the fact that implicit conversion failed.
So, I'll commit "no implicit conversion of into " unless there's a better wording proposed
- Status changed from Open to Closed
- % Done changed from 0 to 100
This issue was solved with changeset r38979.
Chris, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like0Like0Like0