Why does nil.to_s
return ""
, but nil.inspect
returns "nil"
(when apparently .inspect
uses the .to_s
method)
The "inspect" method of the Object class is supposed to return a human-readable version of the object.
The human-readable version of nil
is "nil" and not "" (which is the human readable version of the string ""
).
That's why nil.inspect
should return "nil", whereas nil.to_s
returns "" in order for "a string" + nil
to return "a string"
as it is generally expected (in other languages for instance).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With