From: Matthew Moss Date: 2006-02-09T01:16:26+09:00 Subject: Re: boolean annoyance > 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. It's actually the most illogic part of languages like C that treat 0 as false. Believe me, having spent most of my programming life using C/C++ and being used to that, I make mistakes regularly whilst writing Ruby code. 0 is an integer and quite often a valid value. C's treatment of 0 as false is convenient in some situations but horribly inconvenient in others. Because if I'm expecting an integer and zero is a legal value, then you have to start playing around in your conditionals... "Okay, today any -1 is false"... or even at different levels than that. Part of the problem is that C doesn't have an actual NULL... NULL is just defined as zero. Overlap, explosions, crash and burn... > Because of this stuff like > > if flags & 0x01 > # do some stuff if flag is set > end There are alternatives.... In C/C++, this is what I would do because it is convenient. In Ruby, I might do: if (flags & 0x01).nonzero? Course, I probably wouldn't hardcode 0x01 in any language, so: if (flags & kSomethingEnabled).nonzero? But I'd even go a bit further (and often do this in C++ for clarity): def something_enabled? (flags & kSomethingEnabled).nonzero? end if something_enabled? # do something end Or even (in Ruby w/ symbols): mask = { :something => 0x01 } def enabled? feature (flags & mask[feature]).nonzero? end if enabled?(:something) # do something end You could even go so far to package this into a bitfield class (if there isn't one already). Point is, there are lots of better ways to get what you need done without sacrificing the value 0 to the old-skool gods. (Who I get along with just fine usually, but not with Ruby's zero.)