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

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

    "2" * 4
> "2222"
parent Zak 8 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton