r/ProgrammerHumor 20h ago

Meme iHateThisPassiveAgressiveBehaviourRuby

Post image
2.5k Upvotes

269 comments sorted by

View all comments

432

u/deceze 20h 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.

116

u/CoroteDeMelancia 20h ago

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

35

u/AwkwardWaltz3996 17h ago

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

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

-1

u/amacks 8h 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

44

u/bhechinger 19h 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.

28

u/UsAndRufus 19h 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.

26

u/bhechinger 18h 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. 😛

u/SilkeSiani 3m 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.

6

u/laplongejr 14h 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.  

5

u/suvlub 16h 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 12h ago

I see why it's a bit confusing.

2

u/rmatoi 15h ago

You're gonna hate bash

11

u/bhechinger 14h ago

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

1

u/Wertbon1789 10h 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.

64

u/Bomaruto 19h ago

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

8

u/R3D3-1 9h ago

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

5

u/Wertbon1789 10h 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/didntplaymysummercar 9h ago

C#, and Java (where C# got it from, probably), Go and Pascal, Rust and Haskell (I mean, with those two's type systems, imagine they didn't forbid it.. lol), it's not even uncommon.

1

u/Wertbon1789 4h ago

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

7

u/suvlub 16h 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

28

u/theturtlemafiamusic 15h ago

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

5

u/ntfaw 16h 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

25

u/Dick-Fu 18h 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

15

u/deceze 18h ago

You are not wrongish.

53

u/Ok_Beginning520 20h 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

21

u/DeepDay6 20h 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.

14

u/hongooi 20h ago

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

15

u/DeepDay6 20h 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 ;)

14

u/Original-Ad-8737 19h 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

8

u/PM_ME_BAD_ALGORITHMS 19h 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 19h 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 19h 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.

5

u/deceze 18h 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 19h ago

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

14

u/requion 20h 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 19h ago

They just did.

6

u/deceze 19h ago

Did… did I break Ruby?

SORRY GUYS, MY BAD!

6

u/Abject-Kitchen3198 19h ago

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

1

u/bugo 16h 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 12h 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 11h 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 18h 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/didntplaymysummercar 16h 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.

2

u/transcendtient 15h 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 8h ago edited 6h 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? Except no - other string representations of 0, which numify to the same thing, are true. Like “0.0” and “0e0”: Only the specific sring "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.

1

u/MattieShoes 17h ago

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

1

u/granadesnhorseshoes 16h 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 9h 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 9h 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/DajBuzi 8h 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 6h 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 6h ago

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

1

u/thaynem 2h ago

This is something ruby got from lisp. 

1

u/postexitus 19h ago

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

0

u/Feisty-Summer9331 13h 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 37m 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 11h ago

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