Why Treat 0 as True in Ruby

Why Treat 0 as True in Ruby?

I'm guessing that Matz wanted conceptual simplicity of "truthiness" as such - the only "false" values are false and nil. Period.

Using just false would be the cleanest but there is understandable need for including nil. To include the integer zero as a special case might open the mental floodgates of questioning truthiness of other types. What about strings, is "" false? And arrays, is [] false? And hashes, is {} false? Ad insanitum (see JavaScript)...

Why can't I do if zero in ruby?

In Ruby only nil can be considered as analogue to false. There is no assumption, that 0, "" or [] means false, so you must use == 0, .blank?, .empty?, .zero?, etc.

But nil doesn't always behave as false. For example, in string interpolation, the .to_s method is applied to #{} contents, that works differently for FalseClass and NilClass:

irb(main)> "qwe #{false} rty"
=> "qwe false rty"
irb(main)> "qwe #{nil} rty"
=> "qwe rty"

Ruby: why does =~ evaluate to true in an if statement?

The predicate after if will be treated as a boolean value.

In Ruby, only false and nil are treated as false. Anything other will be evaluated to true, so 0, [], {} are all true in boolean context.

From Ruby doc:

nil and false are both false values. nil is sometimes used to indicate "no value" or "unknown" but evaluates to false in conditional expressions.

true is a true value. All objects except nil and false evaluate to a true value in conditional expressions.

And you may have a look at the control expressions.

Best ruby idiom for nil or zero

Objects have a nil? method.

if val.nil? || val == 0
[do something]
end

Or, for just one instruction:

[do something] if val.nil? || val == 0

What is good if an empty string is truthy?

I think I came up with the answer. Probably, it is for theoretical reason and for efficiency. Drawing a line between different instances of a single class is theoretically more complicated than drawing a line between classes, and would require more computation. If "" were falesy, then the Ruby internal would have to check the string's length each time there is a string in a condition. Instead, if the truethyness can be judged just by the class, which happens to be the case, then it would be much more efficient. Perhaps this is also the reason why false and true are not subsumed under a single class Boolean but belong to independent classes respectively.

Another possibility is that there are only one instances of false and nil, so dealing with that internally in Ruby would only require to check the pointer/object id, whereas strings are mutable, and would require more complexity to check for emptyness.

Ruby lazy if statement with no operator

sure, it's possible

everything except nil and false is treated as true in ruby. Meaning:

var = 0
if var
# true!
end

var = ''
if var
# true!
end

var = nil
if var
# false
end

Why does this evaluate to false? S == /[S]/ = 0

"S" == /[S]/ is false because == in Ruby doesn't evaluate whether a regexp matches, it just determines whether two objects are equal. The string "S" and the regexp /[S]/ are of course completely different objects and not equal.

=~ (which is a correct way to match a regexp against a string in Ruby) returns the match position. In your first example the match position is the beginning of the string, 0. In the second example there is no match, so =~ returns nil.



Related Topics



Leave a reply



Submit