Ben Giddings
7/14/2003 10:18:00 PM
On Mon July 14 2003 5:17 pm, Austin Ziegler wrote:
> On Tue, 15 Jul 2003 05:34:02 +0900, Mark J. Reed wrote:
> > I would also be happy if the test for falseness was less simplistic
> > than "false is false, nil is false, everything else is true". I am
> > constantly being bitten by the fact that 0 is true.
>
> If you *know* that you will be getting a Fixnum in response, you can use
> Fixnum#nonzero? and Fixnum#zero? to get the tests that you're after.
I don't think the issue is that it is difficult to come up with a test to
determine whether or not a number is zero or nonzero, it's just whether
Ruby's treatment of "false" and "nil" as false and 0 and the empty string as
true makes sense, or if it should maybe be more flexible.
I happen to like 0 and "" being logically "true". It sometimes requires extra
typing but that's fine. It's very lisp-ey in that only nil is false,
everything else is true, only Ruby does have boolean classes, and in that
case the literal "false" should of course be false.
While I also kind-of like the idea of a "to_b" method that can determine if a
class is logically false, I see it causing lots of problems. If that were
possible, someone might take advantage of Ruby's flexibility to make 0.to_b
be false for their program. This could lead to some real difficulty in
understanding or running other people's code.
I prefer to be overly anal about the whole thing and always just say:
if 0 != retval ...
Ben