"Mark Hahn" <mchahn / facelink.com> writes:

> Why does puts(1) work, but puts('s'+1) not work?  It gives:
> 
> 	in `+': failed to convert Fixnum into String (TypeError)
> 
> The error message makes it clear that it knows it should be converted to a
> string.  Why didn't + method just do a .to_s to the parameter coming in?

#puts knows that it it's parameter is not a string, it can call
to_s. However, in general, matz decided that allowing arithmetic on
arbitrary strings leads to too many errors, so he did not add
string coercion to the standard arithmetic operators. You can always
add it if you'd like (putting it into a file you could require would
make it reusable across all your programs).

> This simple fix would match Java and allow very simple string
> constructs.

Java has static typing, while Ruby does not. The two require a
different mindset when coding.


Dave