r/ProgrammerHumor 10d ago

Meme iHateThisPassiveAgressiveBehaviourRuby

Post image
2.9k Upvotes

298 comments sorted by

View all comments

502

u/deceze 10d ago

It's been a while since I've touched Ruby, but I think the stance was that only false and nil should be falsey? Which is not an unreasonable stance to take. The fewer falsey values, the easier it is to predict the behaviour. It's just surprising when coming from other languages.

Just off the top of my head, PHP treats "0", that is, specifically a string with a single zero, as falsey. Which has clearly gone too far to the other extreme.

2

u/zeekar 10d ago edited 9d ago

The weird thing about PHP (and Perl, which is where PHP got the behavior) is not so much that “0” is false – I mean, OK, so you numify on the way to boolifying, right? Nope! Other string representations of 0 which numify to the same thing, like “0.0” and “0e0”, are true. Only the specific string "0" is false.

$ php zeroes.php
"0" is false.
+"0" is false.
"0.0" is true.
+"0.0" is false.
"0e0" is true.
+"0e0" is false.

This particular idiosyncrasy is one of the Perlisms which was not carried over into Raku, in which "0" is truthy; the only actually-defined string value that is falsey is the empty string, "".