Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> 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
> 8

Interestingly, it is possible to multiply a string by a number, but no coercion takes place.

    "2" * 4
> "2222"


OP here - Yes that's the operation the question wasn't supposed to be a literal one, rather a consistency issue, which illustrates the larger point that different languages deal with null differently because it's not logical and therefore confusing and a pain in the ass :)


Languages are definitely not consistent in what they do if you try to coerce null to various types, nor are they consistent (often even internally) about whether they perform coercions implicitly. The Ruby example seemed to deal mostly with the latter.

I think implicit coercion is bad, but that's a separate issue from null.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: