45

I'm reading through the excellent Ruby on Rails Tutorial and have encountered the following code.

if 0 true else false end 

The above returns true and illustrates how unlike many languages (C being the obvious example), Ruby treats 0 as true. Rather than dismiss the behavior as idiosyncratic, I assume there is a good reason for this significant departure from convention. Python, for instance, treats 0 as False, just as one would expect.

In short, what is the rationale in designing Ruby to treat 0 as true?

0

3 Answers 3

66

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)...

Sign up to request clarification or add additional context in comments.

5 Comments

What's interesting, especially since I'm coming to ruby from python, is that all the examples you name ([], {}, and "") are false in python. In my view, this only enhances the quirkiness of ruby.
And I'm sure people learning Python after knowing Ruby, Perl, JavaScript, Lua (etc.) find it quirky that those values aren't true!
Rails introduced #blank?, #present?, #empty?, and maybe more because, i think, 0, [], {}, and "" are not false. That make code bigger against ruby's less code principle.
in ReactJS (Javascript), I wrote baseAmount && <MyComponent> and suddenly my component disappeared when baseAmount was 0 as 0 is falsey in Javascript.
all your examples can be easily categorized as falsy without any danger, since they're pretty much the only ones; where it might get a little hairy is if you wanted to categorize stuff like [""] or {[]} as falsy but I'm not aware of any languages that do that
15

In ruby, if exists, it's true. If not, it's false.

so, with Ruby null(no address assigned) and false are only false.

All others are true because it has address assigned to it.

I think of this way; "Does it exist?"

3 Comments

what you said is not true: nil is an object, so is false, and both of them exist :), they are instances of NilClass and FalseClass. the only false values are false and nil.
...i.e., "except for" false
ok, yes everything is object in ruby. nil is the one and only instance of the NilClass class.I don't think it really matters because it's pointing to a instance that has null pointer. I regard this as 'not exist'.
7

In Common Lisp, 0 is also treated as true. For example, the following code returns true.

(if 0 'true 'false) 

No doubt, Ruby is following the same design decision made in Lisp. In Lisp, only an empty list (represented by nil) is false.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.