Hello,
This is a small thing, but one that has ever-so-slightly annoyed me for
quite some time now. Instead of generating a TypeError on non-string input,
why not just call to_s on it? Isn’t that always what you want? Even as a
newbie, all those years ago, I still felt like there was this inconsistency:
print ‘hi’,5 # No problem.
print “hi#{5}”# All good.
print ‘hi’+5 # TypeError!
I always assumed there was some good reason for it (even if it was a bit
confusing, and probably the source of fully half of my newbie mistakes, and
which requires a disproportionate amount of my tutorial to explain…).
However, if there is a good reason for it, I still don’t see what it is.
(Mind you, I wouldn’t be surprised if there was one, but at this point I’d
also be surprised if there wasn’t one.)
You might be saying to yourself, "Why doesn’t he just use substitution?"
Well, frankly, I don’t know how everyone else uses substitution all the
time! I think it’s ugly and avoid it whenever I can. (Pretty much, I only
use it for heredocs.) Since nearly all newbies agree with me and nearly all
experienced rubyists disagree with me on that point, perhaps I’m still
clinging to my inner newbie self.
In any case, how 'bout it? Let’s save newbies lots of needless TypeErrors
(“cannot convert fixnum into string?? geez, even I can do that…”), and
save me lots of needless ".to_s"s.
Chris