r/ProgrammerHumor 10h ago

Meme iHateThisPassiveAgressiveBehaviourRuby

Post image
2.0k Upvotes

244 comments sorted by

879

u/pydry 10h ago

Correction : JavaScript is the weird language people actually use.

206

u/deceze 10h ago

Fact: programming as a profession might be the single weirdest thing humanity has ever done.

199

u/Nimeroni 10h ago

What, you means it's weird we instruct daemons made of lightning in a rock with an arcane language ?

54

u/Lucho_199 9h ago

Funny that it sounds so epic

https://giphy.com/gifs/fX8771PO1eATJz6r4R

27

u/evorm 5h ago

That's cause it is pretty epic when you think about it. We have made lifeless objects we can bestow with our intelligence, and they in turn can affect the world tangibly using only the mere inscription of information.

15

u/laplongejr 5h ago

For the anti-epic version XKCD#722 has us covered

5

u/Confident-Ad5665 9h ago

Those of us drawn to it certainly are.

24

u/Weak_Inflation9120 7h ago

"There are only two kinds of languages: the ones people complain about and the ones nobody uses,” Bjarne Stroustrup. As much as I hate JS, he has a point

64

u/oldsecondhand 10h ago

Addendum: all script languages are weird.

30

u/Striky_ 10h ago

Correct but somehow some managers decided it is the correct language to base every new technology on... Somehow

8

u/oldsecondhand 9h ago

I'm glad that at least Rust and Kotlin are getting some mindshare.

1

u/foonek 9h ago

Is kotlin though?

8

u/oldsecondhand 8h ago

On Android it's popular, and some teams use it on backend too.

3

u/foonek 8h ago

I haven't seen kotlin used on the backend ever, personally. Of course sample size: 1.

I can see it being more popular on android though indeed

3

u/oldsecondhand 8h ago

I've seen it once in an investment bank, and the solution didn't even have an Android project. It was just infrastructure for other backend projects.

2

u/Vidimka_ 6h ago

Good for them! Ive been enjoying Java since start but one day Kotlins blessing has come upon me and its so great. It gives so much Daniel / Cooler Daniel vibes! Wish more companies would prefer Kotlin at least for new projects

2

u/SalamanderSylph 5h ago

I'm in an IB and we use Kotlin for backend services

1

u/Loading_M_ 5h ago

To be fair, Java on the backend is also getting less common as well. Rust and Go are taking more of the space.

JS also has a huge market share, but I don't have to be happy about it.

1

u/oldsecondhand 5h ago

Not in finance. Retail banking and most of investment banking runs on Java.

2

u/eo5g 7h ago

Addendum: all languages are weird.

2

u/Maleficent_Memory831 2h ago

Except for 8051 assembler language. It's merely eccentric.

1

u/Maleficent_Memory831 2h ago

Ruby though (ignor the on Rails gibberish) is just a textual form of Smalltak. With a few warts.

23

u/ClipboardCopyPaste 10h ago

Opinion: Every programming language that provides some kind of abstraction is weird.

18

u/foonek 9h ago

Even assembly is an abstraction. Going deeper leaves you without a programming language

8

u/PlanetVisitor 9h ago

going deeper will leave you without ...any (type of) language

going deeper will leave you without ... anything

3

u/foonek 9h ago

That's very deep

1

u/ttlanhil 3h ago

You have machine code, still a language of sorts, but very low level and not at all portable. But once upon a time that's what programmers wrote

1

u/PlanetVisitor 3h ago

Exactly, but can that be called a language?

1

u/audigex 49m ago

Just me sitting here manually turning switches on and off

1

u/Maleficent_Memory831 2h ago

Hardware is merely organized quantum chaos.

20

u/sadongrohiik 9h ago

JavaScript is actually fine, what's not fine is it being crammed in everywhere it doesn't belong due to sheer laziness. You want to make button do stuff on the web? JavaScript is great. You're processing business logic on the backend? just no. I just do not understand why we'd rather jump through all these hoops just to make JavaScript fit where other languages fit naturally 

3

u/Maleficent_Memory831 2h ago

JavaScript just grew into that role, but being the browser-side language that let you push buttons. Creeping featurism let it be able to do things it should never have been able to do. Features added without a lot of thought and with incredibly weird semantics that were easier to implement without actually making a solid standalone language. It is weird precisely because it grew through accretion rather than design.

It's a wart on the side of a browser that grew and grew until it had arms, legs, and fingers.

2

u/HammerChilli 6h ago

Forgive me because I'm just a junior dev - but when the API is already written in Node or .ts I mean, it's gonna do some business logic with incoming requests, or are you saying back ends shouldn't be in js entirely?

1

u/sadongrohiik 5h ago

There was really never a real reason to use JS on the backend and it wasn't designed for it. There are tons of languages far more suited for backend. For some reason the idea of the entire codebase being in a single language appealed so much to many organizations that we continuously create weird abominations to fit a square peg in the round hole

→ More replies (1)

10

u/woololooo 9h ago

f off. ruby rules. bye.

1

u/thisguyfightsyourmom 49m ago

Did you guys hear something?

1

u/traditional_ant_56 8h ago

Heavily* use

1

u/[deleted] 5h ago

[deleted]

2

u/cross_the_threshold 5h ago

What? Ruby on Rails is what Twitter was originally built on. Huge platforms are still using it. It was a top ten language in 2015, and though it’s far from its peak it’s quite stable now. Do you only talk to Java and Python devs?

I mean hell have you heard of GitHub??

1

u/Robo-Connery 4h ago

Why make a website with JavaScript when ruby on rails exists.

1

u/thisguyfightsyourmom 50m ago

I’ll never forget the time I interviewed the hotshot digital agency in town for a job and the owner scoffed hard and gloated over Ruby’s supremacy vs the Node ecosystem in terms of app development when I said I saw myself making a career of web app development with JS.

Then React launched, and I only think about him when people mention Rails now.

543

u/lazyzefiris 10h ago

You need to at least have heard of the language to hate it. These kids only heard of Javascript.

62

u/Cherry-PEZ 10h ago

Wild too, ruby used to be everywhere

52

u/DrMaxwellEdison 10h ago

Still is, just silent in the background chugging along.

32

u/chem199 9h ago

It is not as prevalent as it used to be. RoR was the framework du jour for a while, and I remember a ton of places using Capybara and Watir for AT work. You don’t see the demand for those as much in my corners of the industry.

11

u/DrMaxwellEdison 9h ago edited 9h ago

Agreed, it's not the most popular starting point for some folks these days, but development of Rails and Ruby itself keep going. I see a few job postings here and there for it, you see some companies starting up with slightly older CTOs still reaching for it cuz they know it best and it works well. Rails' creator, DHH, even makes a point of Ruby being one of the most token-efficient languages for AI tools, for what that's worth.

Again I know it's not the most popular stack these days, it's certainly not everywhere anymore. I still think some folks would be surprised at knowing it's still chugging along and far from legacy stuff.

15

u/chem199 8h ago

It’s always weird to remember that GitHub is still very much on the Rails train.

6

u/DrMaxwellEdison 5h ago

Don't remind them or Microsoft will try to rewrite it in typescript. /s

1

u/DDFoster96 1h ago

Surely rewriting it in typescript can't make the uptime any worse than they manage with Ruby?

1

u/arthurno1 25m ago

Even Cobol is still chugging in some places, but I am not sure what that proves. I am that old to remember when Ruby was launched, and I remember the hype around Ruby on Rails, and Ruby everywhere. It was backnin golden days of Java and OOP, but I never really jumped on the train personally. If Ruby was invented today, it would be probably just one of many posts in /r/programminglanguages.

u/Resident-Trouble-574 8m ago

It might be token efficient when reading and generating code, but if it's interpreted and dinamically typed you quickly lose that advantage when the AI need to repeatedly run and test the application to find bugs that would have been easily caught by a compiler.

→ More replies (1)

6

u/DuhMal 9h ago

It was wild having to learn Ruby to play around in RPG Maker XP as a kid

3

u/Cherry-PEZ 8h ago

Holy shit forgot that was ruby, I learned it for the old (not corpo) metasploit framework during my script-kiddie days.

97

u/Embarrassed_Death69 10h ago

I love JS, and I cannot lie, those hippies writing clean Rust cannot deny

7

u/locri 9h ago

No matter how bad you think Java is I'm sure someone works with someone so ineffective or insane that they could make anyone hate anyone's favourite language.

Bad code is written by people.

9

u/Holek 10h ago

Sheesh, shots fired

6

u/NecessaryIntrinsic 10h ago

You mean java?

/s

6

u/GOEDEL_ESCHER_BOT 10h ago

Java is just JavaScript with less letters

3

u/gerbosan 9h ago

Oh, you mean the long import names?

3

u/deceze 9h ago
Question question = new Question("What do you mean?");

2

u/GSide901 9h ago

He saw one truth table in JavaScript and decided the entire programming world was wrong 😭

1

u/Keebster101 7h ago

A wise man once said, there are two types of programming languages:

Ones people hate
And ones no one uses

1

u/csabinho 5h ago

You mean JavaScript doesn't have craptons of historically necessary weird behaviours?

1

u/lazyzefiris 5h ago

I'm curious about the leap that happened in your head between "You need to at least have heard of the language to hate it" and "JavaScript doesn't have craptons of historically necessary weird behaviours". Please elaborate in detail.

1

u/csabinho 5h ago

It sounded like you defended JavaScript by this. Didn't you?

3

u/lazyzefiris 5h ago

This is r/ProgrammerHumor . We are poking fun at each other (and JS).

359

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

51

u/Bomaruto 9h ago

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

3

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

3

u/R3D3-1 32m ago

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

8

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

22

u/theturtlemafiamusic 5h ago

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

5

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

94

u/CoroteDeMelancia 10h ago

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

27

u/AwkwardWaltz3996 8h ago

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

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

29

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

21

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

16

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

3

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

3

u/rmatoi 5h ago

You're gonna hate bash

6

u/bhechinger 5h ago

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

1

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

2

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

I see why it's a bit confusing.

20

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

10

u/deceze 9h ago

You are not wrongish.

50

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

13

u/hongooi 10h ago

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

13

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

16

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

14

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

4

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

5

u/atanasius 10h ago

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

15

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

They just did.

7

u/deceze 10h ago

Did… did I break Ruby?

SORRY GUYS, MY BAD!

4

u/Abject-Kitchen3198 9h ago

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

→ More replies (3)

2

u/immortal_lurker 9h 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 7h 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 6h 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.

1

u/MattieShoes 8h ago

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

1

u/granadesnhorseshoes 6h 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/Arshiaa001 1h ago

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

u/Resident-Trouble-574 2m 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.

→ More replies (2)

121

u/ReefNixon 10h ago

We invented boolean types so that we could stop treating 0 as false.

22

u/Embarrassed_Death69 10h ago

"What do we make of empty strings John?"

35

u/mbmiller94 10h ago
if ("") {
    print("what?.........if what??");
}

5

u/Koeke2560 2h ago

This is legit the first joke code in my life that made me actually laugh

2

u/UristMcMagma 7h ago

And so that we could stop treating 1 as true. The only real argument for type coersion is convenience. I don't see how it's more convenient to treat 0 as true rather than false.

6

u/ReefNixon 6h ago

If we take coercion as a given, what if your conditional is the index of an element in an array? If it doesn’t find it, it returns nil (which is falsey). If it’s the first element, it returns 0. Shouldn’t that be truthy?

1

u/UristMcMagma 6h ago

Yes. I'm not really a Ruby guy but after reading up I can see how it's more convenient to treat 0 as truthy in that lang. in js you never use findIndex since find is just better.

2

u/ReefNixon 5h ago

There are language agnostic examples too, like when tracking things that don’t stop at 0 e.g. temperatures.

It would be weird if temperatures under 0 degrees and temperatures over 0 degrees coerced into truthy, but exactly 0 degrees was falsey.

Coercion will always have some level of ambiguity, Ruby just chooses to only treat definitely falsey values as falsey (literal false and nil).

2

u/UristMcMagma 5h ago

Temperatures do stop at 0 if you're using the correct unit. ;)

But yeah I agree with you. There's always some level of ambiguity caused by coersion. Even treating nil and false the same for a boolean is technically wrong, but we can accept the ambiguity for the sake of convenience.

1

u/Clen23 5h ago

My take exactly, the only situation where you'd want to use 0 to mean "false" is if you're using lower-level logic.

49

u/stipo42 10h ago

As someone who has never written any ruby, I can understand the desire for any non null value being truthy.

I like golangs approach though where there's no null value for anything that isn't a pointer

6

u/prehensilemullet 6h ago

any non null value being truthy

Even false?

3

u/Goatfryed 5h ago

every. non. null. every. Can't you read?

3

u/captainAwesomePants 3h ago

Golang doesn't have "truthy" as a concept. You have to be explicit. A null value isn't true or false because only booleans can be true or false, and a null pointer isn't a boolean. You can have a pointer to a boolean, but you still have to check explicitly for null. In specifically Go, u/stipo42's preferences for truthiness are moot.

In a language like Python, with truthiness, u/stipo42's preference for false to be truthy is insane, and he should feel bad.

13

u/SukusMcSwag 9h ago

"Golangs approach" you mean compiled languages treating a scalar as a scalar?

37

u/American_Libertarian 10h ago

Same in Bash. Zero is truthy because executables exit 0 on success, and exit nonzero on error

3

u/oweiler 8h ago

More like in Perl, which Ruby took heavy inspiration from.

1

u/HandleUnlikely2589 2h ago

Ruby didn't follow Perl at all though when it came to boolean evaluation. Perl considers "0" (the string containing the character 0) to be false (because there is no difference between "0" and 0). It has a special value, the string "0 but true", to allow returning a value that is numerically zero (and doesn't raise warnings when used as a number, unlike most strings that aren't numeric), but boolean true. Strings and numbers interoperate seamlessly depending on the operator used, super weakly typed.

Ruby went the opposite way, and said false and nil are falsy, everything else is truthy, and aside from that, all types are super strict (no adding numbers and strings at all, you need to convert explicitly).

31

u/TKristof 10h ago

Implicit type coercion is just bad in general and shouldn't be allowed, that includes booleans. 0 and false are not the same thing even if in memory they (usually) are represented the same. Just because 40 years ago we thought it a good idea doesn't mean we shouldn't improve on that. And I say this as someone who has programmed quite a bit in C and C++.

I could maybe see an argument for optionals but even there I would disagree and much prefer explicit pattern matching or zig style if syntax for it.

1

u/Spaceshipable 23m ago

I like Swift’s approach to this, even if it does end up slightly more verbose sometimes.

45

u/redlaWw 10h ago

Same in Lua. Only nil and false are treated as falsy, everything else is treated as truthy. The idea is that by default everything that is not nil is treated as true because it exists, and false is a special case used to represent false propositions.

It can be a bit of a stumbling block coming from some other languages, but the rule is so simple that you get used to it quickly.

6

u/NotDuckie 6h ago

Imagine having rules for what should be truthy and falsey instead of just rolling dice every time.

3

u/Tsu_Dho_Namh 3h ago

I love it tbh.

"If (variable)" is a fantastically clean way to check if the variable isn't nil.

13

u/SanoKei 10h ago

variable types being truthy based on validity is not stranger than having to know the scientific table of JavaScript bullshit

10

u/uvero 10h ago

In Ruby, if you want to branch based on whether x is zero where x is a number, you can always check for x == 0, or, if you're feeling cool, x.zero?

4

u/Different_Routine215 8h ago

a basic linter will fail you if you use == over zero?

3

u/uvero 8h ago

Just Googled it and apparently yes. Nice idea in my opinion - if a language has an idiom on the right way to do something, make a linter tell you that. I wouldn't know myself, I've only ever used Ruby for hobby projects years ago when I just started learning to program. Fun language.

2

u/Different_Routine215 7h ago

set up an autocorrect/autolint on file save and you will pick up good habits quickly

9

u/SleepingInsomniac 8h ago

In Ruby, everything is an object. 0 is just an instance of Integer. Object instances are truthy, compared to say, nil. Makes more sense than ( 'b' + 'a' + + 'a' + 'a' ).toLowerCase() === "baNaNa" or 1 + '0' == '10' or NaN === Nan; // false

1

u/metaglot 8h ago

Nan != Nan is part of ieee754. If this isn't the case in ruby, ruby is not compliant.

1

u/SleepingInsomniac 8h ago

Float::NAN == Float::NAN # false til!

→ More replies (2)

7

u/ekauq2000 10h ago

Wait till they find out that in VB true is -1

2

u/AwkwardWaltz3996 7h ago

Well that's simple. Everything is either something or nothing. - 1 isn't nothing so it's something (true)

10

u/Sw429 10h ago

Any language that coerces non-boolean values as "truthy" is weird. That's a major footgun.

3

u/gravitywaveshello 9h ago

You’re absolutely right (unironically)

12

u/traplords8n 10h ago

Bash is the same way but it makes sense considering 0 represents the single route of success, while other numbers can mean different error codes.

I have no idea why ruby does it, but in bash at least there is a solid reason for it.

13

u/Zealousideal_Low1287 9h ago

It’s basically because of this lineage. Perl inherited it from other shell languages, and Ruby is essentially a direct decedent of Perl.

11

u/budgiebirdman 9h ago

This is the thread of truth I came looking for. All other answers are just discussions on whose farts smell the sweetest.

2

u/314159265358969error 9h ago

The bitset representing the number 0 in Ruby is non-zero. (I don't remember which bit it is, but reference/non-reference values are recognised by a specific bit being flipped to 1 if non-reference, which is the case for 0.)

8

u/Serotav 10h ago

nha js can do it aswell, try:

let a = new Boolean(false)

if (a) {
console.log("it was true indeed")
}

→ More replies (3)

3

u/BusEquivalent9605 9h ago

“there are the languages people complain about. then there are the languages that nobody uses”

3

u/drkspace2 9h ago

All truthy values are bad. Be explicit about what you're testing.

7

u/varsderk 8h ago

I build programming languages.

0 should be truthy. Only false and nil/null should be falsey.

1

u/HungryCaterpillers 7h ago

Then you build shitty languages

2

u/Appropriate_Emu_5450 5h ago

Unironically agree.

→ More replies (1)

6

u/Single-Virus4935 10h ago

Golang: "wie dont do that here" meme

3

u/deceze 10h ago

Wie jetzt, warum nicht?

1

u/Single-Virus4935 7h ago

Golang konvertiert keine typen implizit.

Du kannst einem int keinen int32 zuweisen ohne explizit zu casten int(i).

Boolean conversions gibt es gar nicht:

```

i := 1

if i {

fmt.Println("Hello, 世界")

}

}

//

./prog.go:8:5: non-boolean condition in if statement

```

```

i := 1

if bool(i) {

fmt.Println("Hello, 世界")

}

// ./prog.go:8:10: cannot convert i (variable of type int) to type bool

```

5

u/ByteSizedFounder 10h ago

Ruby said "zero is truthy" one time and JS has been living rent free in everyone's head since, acting like it is not the unstable one in this relationship

5

u/sadongrohiik 9h ago

0 is truthy in a lot of languages like Elixir, clojure, Lua, and every other lisp variant. And the logic is that only explicit false or nil are falsy. This isn't the same as JavaScript's random behaviors

3

u/Klizmovik 9h ago

Behavior is not random at all if you read the documentation properly.

2

u/CyberMoogle 10h ago

0 in JS is falsy

2

u/BigSwagPoliwag 10h ago

As a Javascript stan, this is actually something that I dislike ABOUT javascript… all JS vars are nullable so I wish I could just write if (!myNum) for null checking, but JS needs to be hella opinionated about truthiness. Fuck that jazz, Brendan Eich can suck my balls.

2

u/walkerspider 9h ago

“0” is truthy in JS. Ruby has a more straightforward set of falsey values, nil and false

2

u/Pleasant-Form-1093 9h ago

You aren't forced to use ruby in day to day life. But almost all web dev uses some javascript or the other

→ More replies (1)

2

u/Dongfish 8h ago

The fact that amounting to nothing can be a positive somewhere gives me hope.

2

u/Vollgrav 8h ago

Ruby's truthiness and coercion rules are one of the best and most consistent ones. And definitely the best in the dynamic languages I know, and I know quite a few.

Treating zero as false was only a thing because in C int was the base type, and other types (char, bool, pointer) were all expressed in terms of ints. It kind of made sense then. But now we know very well that abstraction that separates us from the hardware is a good thing. And with this assumption, treating zero as false is only a stale habit from the ancient days. Let alone monstrosities like treating 0.0 as falsy. Ruby did exactly the right thing here by cutting all that crap.

2

u/geeshta 7h ago

Funnily enough, this is what Gödel used when converting formal logic statements to programming. 0 meant the statement holds and 1 meant it is false.

2

u/SeriousPlankton2000 6h ago

Let the BASHing begin

2

u/0xffff-reddit 6h ago

The fact that both languages have something like truthy and falsy makes them both weird.

2

u/Typical_Ad_2831 4h ago

In bash, 0 is truthy, and everything else is falsey...

6

u/creme_de_marrons 9h ago

What kind of moron would need 0 to be falsy?

If you need that use a fucking boolean.

Obviously, 0 is truthy, in fact if you have anything it's truthy. This way you don't have any weird ass side effect and you can use ||=

1

u/GoogleIsYourFrenemy 9h ago

Wat

It's relevant.

1

u/caleblbaker 9h ago edited 5h ago

When I use non-Boolean values in a Boolean context I like them to coerce to compile time type mismatch errors. Because making me fix my mistake is better than trying to guess my intent based on a typo.

1

u/AwkwardWaltz3996 7h ago

Ruby is pretty precise with it's types.

Python which most people know now is definitely written to be more flexible. Which is nice when you're new but is a big headache once you build out. I used to hate static typing for example as it was obtuse, but as soon as you are working with others, you realise their use. People will use "hacks" at the slightest inconvenience and break everything.

0 isn't nothing, it's a number. And therefore needs to be handled as such.

1

u/FredodaFred 6h ago

He forgot the comma and it made me have an aneurysm

1

u/skcortex 6h ago

Oh kids these days don’t know about Perl,do they? 🤣

1

u/stools_in_your_blood 5h ago

Not sure why any language designer would use the concept of truthy and falsey. All that potential for confusion and bugs so you can write "if (x)" instead of "if (x != 0)".

1

u/TheTroll007 5h ago

I mean 0 is not nothing, so you can do "if x" and if x is not false or nil, you get true. I kinda like this approach.

→ More replies (3)

1

u/cuterebro 5h ago

In Ruby you can patch this behavior.

1

u/GNUGradyn 5h ago

The real mistake here is coercing types automatically in general. Whatever happened to fail-fast/fail-hard?

1

u/j0hnp0s 5h ago

Weird languages is a set, not a competition

1

u/Fidodo 3h ago

JavaScript isn't even that weird when you add type safety from type script and forbid the weird parts with linters.

1

u/LOST_GEIST 2h ago

Javascript is kinda like public transit; the loudest complainers are the people that use it the most.

1

u/Desperate_Formal_781 1h ago

Why do people keep saying truthey and falsey? Do they mean boolean and false? Are they 5 years old?

1

u/MrFrog2222 1h ago

ye, cuz 0 == "0" also comes out as true

1

u/NebNay 1h ago

I'll get downvoted again but here we go: zero should be truthy. It's a defined value, why would it be false? How is zero different than one?

1

u/BraxbroWasTaken 1h ago

0’s truthy in Lua too.

1

u/oasis9dev 47m ago

I'm not a fan of Ruby personally but this isn't weird, it should be expected. Why are we coercing values when we could be explicit and reduce edge cases?

1

u/MountainNinja6432 43m ago

0 or “0”?

0

u/Key_Conversation5277 10h ago

But wtf is a "truthy" value? It doesn't make any sense nor mathematically nor programmatically

8

u/cosmo7 10h ago

Dynamic typing is like the dark side of the Force. At first it feels more powerful, but then it turns you into a murderous psychopath.

→ More replies (1)

2

u/deceze 10h ago

Any value that causes an if statement to run its associated block, instead of going to else/doing nothing.

→ More replies (4)
→ More replies (8)