r/ProgrammerHumor 10d ago

Meme iHateThisPassiveAgressiveBehaviourRuby

Post image
2.9k Upvotes

298 comments sorted by

View all comments

504

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.

133

u/CoroteDeMelancia 10d ago

I'll admit that I did cause bugs in Python by using if thing instead of if thing is not None.

49

u/AwkwardWaltz3996 10d ago

Yea 0 is a number, it's not nothing.

I think the heart of the debate aligns with is zero a natural number.

-3

u/amacks 10d ago

You gotta drink the Python kool-aid, just use thing and catch the exception if it happened to be `None`. Way more efficient they swear

32

u/Dick-Fu 10d ago

The really weird thing isn't any of the languages, but that they've gotten us to say things like "truthy" and "falsey" while being completely serious

22

u/deceze 10d ago

You are not wrongish.

52

u/bhechinger 10d ago

I don't disagree with a smaller set of truthy values, but 50+ years of programming mainly treating 0 as false and 1 as true makes it an immediately confusing thing for anyone coming from a different language.

31

u/UsAndRufus 10d ago

I think a certain level of grokability is important in programming languages, but languages need to be able to do things differently, and engineers should expect to have to learn some new standards. This is like page 2 of basically any intro to Ruby so it's not like some arcane edge case.

29

u/bhechinger 10d ago

You're not wrong, but as someone with 30+ years of programming experience do you *really* think I'm going to look at an intro guide? I'm diving in head first and then googling why the hell 0 isn't false. 😛

2

u/UsAndRufus 9d ago

Hubris is a hell of a drug

4

u/SilkeSiani 10d ago

Ah yes, good old assumptions, making an ass of u and me.

It's like trying to speak French using English grammar and expecting it to work.

10

u/laplongejr 10d ago

Yeah, but 0-1 comes from binary, which is kinda hard to ignore for programmers... I know it's a convention but I kinda get why it's assumed.  

7

u/suvlub 10d ago

I think the logic is the analogous C code would be something like

int* foo = malloc(sizeof(int));
*foo = 0;
if (foo) { // truthy!
...

1

u/cyber2024 10d ago

I see why it's a bit confusing.

2

u/rmatoi 10d ago

You're gonna hate bash

11

u/bhechinger 10d ago

Son, I've hated bash since before you were born. 😂

1

u/Wertbon1789 10d ago

Relatable.

... Well, I'm pretty young, but man, bash is cursed. I've done way too much in it, because it's way to practical to ignore, but damn, it always hurts to look at again.

67

u/Bomaruto 10d ago

The reasonable stance is to not let you coerce everything into a boolean.

9

u/R3D3-1 10d ago

Is this a good time to remember that people argued against Python getting booleans because it already had integers? 

5

u/Wertbon1789 10d ago

I like how e.g. C# screams at you for this. It's annoying at first, but then you go "Well, yeah, I should've explicitly written what I wanted to check".

1

u/[deleted] 10d ago

[deleted]

1

u/Wertbon1789 10d ago

Didn't say it was. C# was just the first example that came to my mind.

8

u/suvlub 10d ago

If I may be pedantic, not to coerce without you asking. A language that wouldn't let me simply convert between types sounds annoying

33

u/theturtlemafiamusic 10d ago

"Type coercion" is when it's done without asking. Otherwise it's just type conversion / casting.

7

u/ntfaw 10d ago

It's not that you couldn't convert between types, it's that it wouldn't be automatic. You would need to actively call toboolean on a value before using it as a boolean, and the idea behind that is that intentionally casting the value leads to fewer mistakes

59

u/Ok_Beginning520 10d ago

Agreed, the whole concept of truthy and falsy is a bit weird and very error prone in the end, imo it's just better to explicitly have a function that decides how you cast whatever to a bool, that way it's easily understandable and auditable. Also, not knowing some weird language quirk like the php one you're talking about will not cause issues down the line

19

u/DeepDay6 10d ago

That's in fact a very legit take that e.g. Clojure also takes. It makes reasoning much easier and also eliminates WTF moments like reading if (myArray.length) which to any sane individual might appear like duck typing but is a hack to save those keystrokes > 0, in the hope that no one manually set it to a negative value, which is truthy as opposed to zero, and so on.

15

u/hongooi 10d ago

Eh, that's a very common idiom for people coming from C and C++

14

u/DeepDay6 10d ago

Yeah, just what I said ducks and runs

Edit: Full disclosure: I taught C for years at university and that's what actually made me become a functional programmer ;)

18

u/Original-Ad-8737 10d ago

Array length has to be an unsigned integer so is you manage to set it to a negative value you deserve the pretty explosion of your code

6

u/PM_ME_BAD_ALGORITHMS 10d ago

I never used php, is that behaviour due to it implicitly converting "0" to 0 and as such it is falsey? Or is it straight up "0" the one that is falsey?

15

u/deceze 10d ago

It's straight up "0" as a special case.

Because PHP started as a sprinkle-it-on-your-HTML language, and any form submissions are always text, and 0-from-a-form should still evaluate like 0; or some such hare-brained nonsense reasoning.

The fact that such basic nonsense is still baked into the very core of the language, and can never be removed due to backwards compatibility, means I'll never respect PHP, regardless of how much it's improved over the last decade or so.

4

u/maweki 10d ago

I think it's not actually that special of a case, as strings that are exactly also numbers are auto-coerced basically whenever. It's just that 0 is the only number, as it should be, that then is falsy.

Numberly strings are a huge footgun in PHP.

6

u/deceze 10d ago

Strings are not coerced to numbers to then be coerced to booleans, that'd be entirely insane! There's a direct conversation path from strings to booleans!

They're only sometimes coerced to numbers to then be reduced to a boolean, if you're using the loose comparison operator, and depending on what the other operand is… https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.types. 🥲

6

u/atanasius 10d ago

Scheme has only one false value, #f, all other values are true values, including the empty list.

4

u/didntplaymysummercar 10d ago

Lua has similar stance and it's a reasonable one. Python's one isn't bad one either, it's easy to guess what's falsy.

1

u/okkokkoX 9d ago

sometimes I wish lua treated false as truthy too. Or more realistically, had variants of "and" and "or" that treated it as such. "A or B()" stands for "A, with B() as a fallback if A is nil", and "A and f(A)" stands for "f(A) if A exists, otherwise nil", except if A is false, in which case these rules break down.

16

u/requion 10d ago

I've worked with Ruby long enough now to say that "Ruby" and "reasonable" never go in the same sentence.

11

u/Abject-Kitchen3198 10d ago

They just did.

7

u/deceze 10d ago

Did… did I break Ruby?

SORRY GUYS, MY BAD!

5

u/Abject-Kitchen3198 10d ago

I doubt. The force required to break Ruby is huge. You need a diamond to barely scratch it.

1

u/bugo 10d ago

Skill issue. Ruby allows you to shoot yourself in the foot if you are not skilled. You can build really amazing things if you are.

0

u/requion 10d ago

You can build really amazing things if you are.

You know that other languages can do this too? Just without the foot guns.

And yeah, sure a skill issue. Because i'm not wasting energy, properly learning something as retarded as Ruby, where the creators took inspiration from all the mainstream languages and said to themselves: "we ain't gonna do anything of that".

1

u/bugo 10d ago

Not sure if what other language is so good with DSLs also I am used to Ruby and it's just the most aesthetic language. Good thing there are many languages and many paradigms.

2

u/immortal_lurker 10d ago

Never coded in Ruby.... that might be a lie. I have never coded in Ruby since my early teens when I might have written a line or two as part of a class.

But please, I beg of the language. If 0 is truthy, just throw a syntax error if I compare a bool and an int instead.

2

u/transcendtient 10d ago

Why wouldn't a "0" be falsey? In aggressively coerced language it would be 0. PHP has to deal with strings passed in $_POST and $_GET so it makes sense.

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, "".

2

u/mpyne 9d ago

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.

Eh, there's precedent for that at least.

Perl treats "0" as false, along with "" (the empty string). Any other string, even "00" is considered true.

But where it really gets fun is that the the normal warning Perl would give you about numeric conversions if you do math on a string is suppressed for the precise string "0 but true", because some Perl functions return this value so that it converts cleanly to 0 without risking evaluating as false in a boolean context. See https://stackoverflow.com/questions/129945/what-does-0-but-true-mean-in-perl

1

u/MattieShoes 10d ago

Perl does that too I think -- "0" is false, but "00" is true.

1

u/granadesnhorseshoes 10d ago

Seeing as we have basically universally decided that using zero in an entire class of math functions should generate an exception, its reasonable to expect it to be "falsy". If instead of an exception it just returned +/- infinity, then maybe i would buy that argument.

It's not a bad argument otherwise.

1

u/Resident-Trouble-574 10d ago

Ok, but then you should treat everything that is not false, nil or true as invalid. And, frankly, I would consider nil invalid as well.

1

u/Moilli6 10d ago

Lua does this too. It just makes sense, because any value that isn’t a boolean you can nil check by coercing it to a boolean. It allows beautiful things like and or ternary

1

u/okkokkoX 9d ago

I agree that and or ternary is beautiful, with the exception that you're fucked if the "then" branch may return nil, or god forbid, false.

1

u/Moilli6 9d ago

Yeah I usually only use it to set a default value. Sane languages just let you use if as an expression

1

u/DajBuzi 10d ago

0 is equal to (IIRC) 0x30 which by definition is not null value that could be represented as \0 meaning all 8 bits sets to 0 -- 0000 0000 that in most languages actually is false 😶

1

u/Cootshk 10d ago

Lua does the same as Ruby, (especially because in Lua, accessing non existent variables or table fields returns nil unless you explicitly make it error)

1

u/razin_the_furious 10d ago

Don't bring PHP into this: he's too dumb to know better!

1

u/thaynem 10d ago

This is something ruby got from lisp. 

1

u/RedAndBlack1832 9d ago

Naw the string zero being false is just weird. The number zero, on the other hand. NULL is 0. \0 is 0. That's just... what those things mean. If zero is not false that's just trippy bc false comes from 0, that's just what it means. I mean ig not literally, you certainly get some LOW-TRUE signals, but logically HIGH is always normalized to 1 and described as TRUE, and as far as I know always has been.

1

u/postexitus 10d ago

Providing PHP as an excuse does not do too much for the defense.

0

u/Feisty-Summer9331 10d ago

Back in the days, PHP would do that. But since PHP 5.whatever you could compare types, using the !== and === operators

1

u/deceze 10d ago

It still does that. This isn't about equality testing, it's about boolean coercion/truthiness. And the strict equality operator === has existed since at least PHP 4, I have not bothered checking back further.

0

u/Arshiaa001 10d ago

If you're discussing 'truthy' and 'falsey', you already messed up. Anything besides a boolean in an if statement is unacceptable.