Comment Comment by Zak > This leads to an inconsistency: if to_i will turn nil into a 0, why won’t that coercion happen when trying to multiply?Because Ruby doesn't automatically coerce arbitrary types to numbers when you try to perform arithmetic on them. This is a Good Thing. 2 * "4" > TypeError 2 * "4".to_i > 8Interestingly, it is possible to multiply a string by a number, but no coercion takes place. "2" * 4 > "2222" parent Zak 8 years ago
Because Ruby doesn't automatically coerce arbitrary types to numbers when you try to perform arithmetic on them. This is a Good Thing.
> TypeError > 8Interestingly, it is possible to multiply a string by a number, but no coercion takes place.
> "2222"