From: Alexis Reigel Date: 2006-02-09T07:35:08+09:00 Subject: Re: boolean annoyance When I started using ruby I had exactly the same problem as you, porting some c code to ruby that was checking some flags. I didn't get to the solution myself, as I never ever thought of 0 being true. But once you see the advantages of it you'll really appreciate that convention. In c you do: (flags & 0x01) In ruby you do: (flags & 0x01 == 0) or (flags & 0x01).nonzero? Or even nicer: flags[0x01] Shortly there was a discussion about the same topic on this list ("nil != []"). I'll cite two convincing postings: matthew smillie.said: 0 *the integer* is only false by convention, and it's a convention confined to programming, originating (unless I'm mistaken) from languages which didn't define specific 'true' and 'false' logcial values separate from integer math. 0's used in some logical notations as a symbol for 'false', but it's unlikely that anyone familiar with formal logic will tell you those 0's are the same 0's you get from "2 - 2". There's no doubt that the convention's been made very useful, but there's really no logical basis for equating any particular symbol to true or false truth values over any other. amrangaye said: One example of this that tripped me up today is the regular expression match operator (=~), which returns (0-based) the index of a match, or nil otherwise. If 0 was false, you couldn't do: if str =~ /^Hello/ Claudio Jeker wrote: > Hello, > > there is one thing in ruby that annoys me most (at least for now). > > if 0 > puts "true" > end > > Yes, I know everything expect nil and false are true but that's probably > the most illogic part of ruby. Because of this stuff like > > if flags & 0x01 > # do some stuff if flag is set > end > [snip]