3 ms·
Yeah... Ruby is in the wrong there. Not even js does that Insisting if takes a bool like java does is perfectly acceptable. But just taking a 0 value and makin
by raverbashing 7y ago
Yeah... Ruby is in the wrong there. Not even js does that
Insisting if takes a bool like java does is perfectly acceptable. But just taking a 0 value and making it "true" makes sense only in Bash?
- lmkg 7y agoIt depends on what the set of falsey values is for that language. E.g. in Common Lisp, the only falsey value is NIL (the empty list). This is easy to understand, easy to remember, and straightforward in practice because idiomatic Lisp does a lot of list operations. It wouldn't make sense for 0 to be truthy, because that's a legit value, and NIL represents the absence of a value. I don't know what values are falsey in Ruby, so I can't comment on it.
- samatman 7y agoLua does this as well, and I quite prefer it. If I return a number or nil, I want to check `if ret`, not `if type(ret) == "number"`. The latter suggests that it might be some other type‡, the former that it might be missing entirely. A language which provides a proper Boolean has no need for the association between zero and falseness, it is a conflation of levels which can lead to subtle bugs. ‡ yes, nil is also a type. I mean some other type of type.
- dnautics 7y agoerlang and erlang VM languages come with 0 being truthy, and the only falsey values are false and nil. I had a junior python programmer disagree with that and within 3 weeks of onboarding him we found a place where it made sense for 0 to be a truthy value. I believe it was a place where we had an options list. One of the options was a value that is settable to zero. In elixir: timeout = options[:timeout] || :infinity if zero is falsy, you can be royally screwed with that code, if timeout should be settable to zero (definitely a thing).
- int_19h 7y agoIt makes a lot more sense when you consider that all values in Ruby would be pointers in C. And if x is a pointer to int, if(x) is still checking whether the pointer itself is null, not whether the int it points to is 0.
- spc476 7y agoLua does that too. Lua only has two values considered false, nil and false. All else, in a Boolean context, evaluates to true.
- bitwize 7y agoScheme recognizes all values except #f, including 0, '() which is how you say nil in Scheme, and "", as true in conditionals. As it should be; having to guess whether something might be falsy is a pain in my rear solved by having precisely one falsy value. As a bonus you can use #f as a sentinel value to denote the absence of a valid parameter or return value when a valid value would be some non-boolean object, and then check for validity with a simple (if x ...)