Ruby Language Design – Why No Implicit Fixnum to String Conversion?

language-designrubystringstype casting

Ruby normally makes things easy. However, it doesn't allow implicitely converting a number to a string:

2.0.0p247 :010 > "a"+1
TypeError: no implicit conversion of Fixnum into String

Why is this when nearly every other language allows it, including Perl, PHP, Java and VBA?

Best Answer

Languages that have implicit conversion to string usually have it for all types, not just numbers. Implicit conversions are usually bad if you want strong typing, type errors that could have been discovered in compile time(in case of compiled static-typed languages) or as exceptions in runtime(in case of interpreted or dynamic-typed languages) may turn into logic errors(the worst kind of error) or unrelated exceptions.

Some languages are willing to deal with this to have a simple string syntax for creating strings, but Ruby doesn't need it because it has string interpolation:

[1] pry(main)> "a"+1
TypeError: no implicit conversion of Fixnum into String
from (pry):1:in `+'
[2] pry(main)> "a#{1}"
=> "a1"