# Booleans

**URL:** <https://rubytalk.org/t/booleans/6845>\
**Category:** ruby-talk\
**Created:** [14 July 2003 20:34 UTC](https://rubytalk.org/t/booleans/6845 "2003-07-14T20:34:02Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_J\_Reed](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mark\_J\_Reed](https://rubytalk.org/u/Mark_J_Reed)\
**Post date:** [14 July 2003 20:34 UTC](https://rubytalk.org/t/booleans/6845/1 "2003-07-14T20:34:02Z")

</div>

Okay, as a convert from Perl to Ruby, I have to say that I love  
just about everything Ruby has to offer. Just about. I wish it had  
better built-in support for Unicode, but that’s a minor quibble.  
However, one thing I really don’t like is the handling of booleans.

I would be happy if the conditionals required actual Boolean  
(i.e., TrueClass or FalseClass) objects and complained otherwise, even  
though it’s rather different from the typeless nature of the rest  
of Ruby.

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.

Ideally, what I’d like to see is the ability for each class to decide what  
constitutes true and false for objects of that class - for instance, a  
to\_b method, by analogy with to\_s. Then you just need three method  
definitions:

```
class Object; def to_b() true end end 
class FalseClass; def to_b() false end end 
class NilClass; def to_b() false end end 

```

And boom, you have the current behavior and the ability to extend it.  
For instance, if you prefer that zero be false, you can do

```
class Numeric; def to_b() self != 0 end end

```

Admittedly, this would break the semantics of =~ etc, but you could  
fix that with a new class for match results that still has 0 as true:

```
class Index < Fixnum; def to_b() true end end 

```

Similar methods would allow empty strings and collections to return  
false, etc. The point is that it would gain a lot of flexibility.  
And a static method like this:

```
def TrueClass.strictlyTrue?(x) x != nil && x != false end

```

would allow the user to override any given class’s definitions to  
make sure that an object is “really” true.

Thoughts?

-Mark

---

<div class="post-metadata">

**Author:** ![KONTRA\_Gergely](https://avatars.discourse-cdn.com/v4/letter/k/3bc359/32.png) [@KONTRA\_Gergely](https://rubytalk.org/u/KONTRA_Gergely)\
**Post date:** [14 July 2003 20:45 UTC](https://rubytalk.org/t/booleans/6845/2 "2003-07-14T20:45:07Z")

</div>

What about not automagically converting everything to boolean?

Gergo

> **···**
>
> On 0715, 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.  
> > –  
> > ±[Kontra, Gergely[kgergely@mcl.hu](mailto:kgergely@mcl.hu) PhD student Room IB113 ]---------+  
> > [http://www.mcl.hu/~kgergely](http://www.mcl.hu/~kgergely) “Olyan langesz vagyok, hogy |  
> > Mobil:(+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom” |  
> > ±- Magyar php mirror es magyar php dokumentacio: [http://hu.php.net](http://hu.php.net) --+

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler2](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Austin\_Ziegler2](https://rubytalk.org/u/Austin_Ziegler2)\
**Post date:** [14 July 2003 21:17 UTC](https://rubytalk.org/t/booleans/6845/3 "2003-07-14T21:17:46Z")

</div>

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.

-austin

> **···**
>
> 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.
> 
> –  
> austin ziegler \* austin@halostatue.ca \* Toronto, ON, Canada  
> software designer \* pragmatic programmer \* 2003.07.14  
> \* 17:15:07

---

<div class="post-metadata">

**Author:** ![Gennady1](https://avatars.discourse-cdn.com/v4/letter/g/a4c791/32.png) [@Gennady1](https://rubytalk.org/u/Gennady1)\
**Post date:** [14 July 2003 20:49 UTC](https://rubytalk.org/t/booleans/6845/4 "2003-07-14T20:49:59Z")

</div>

> > 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.  
> > What about not automagically converting everything to boolean?

Then the following nice feature would not be possible:

@var ||= SomeClass.new

> **···**
>
> ----- Original Message -----  
> From: “KONTRA Gergely” [kgergely@mlabdial.hit.bme.hu](mailto:kgergely@mlabdial.hit.bme.hu)  
> To: “ruby-talk ML” [ruby-talk@ruby-lang.org](mailto:ruby-talk@ruby-lang.org)  
> Sent: Monday, July 14, 2003 1:45 PM  
> Subject: Re: Booleans
> 
> > On 0715, Mark J. Reed wrote:
> 
> > ## Gergo
> > 
> > ±[Kontra, Gergely[kgergely@mcl.hu](mailto:kgergely@mcl.hu) PhD student Room IB113 ]---------+
> > 
> > > [http://www.mcl.hu/~kgergely](http://www.mcl.hu/~kgergely) “Olyan langesz vagyok, hogy |  
> > > Mobil:(+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom” |  
> > > ±- Magyar php mirror es magyar php dokumentacio: [http://hu.php.net](http://hu.php.net) --+

---

<div class="post-metadata">

**Author:** ![Mark\_J\_Reed](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mark\_J\_Reed](https://rubytalk.org/u/Mark_J_Reed)\
**Post date:** [14 July 2003 21:52 UTC](https://rubytalk.org/t/booleans/6845/5 "2003-07-14T21:52:58Z")

</div>

Sure. Or just plain old != 0. But I always forget that I need to do  
anything at all beyond “if (expression returning Numeric)”. After  
20 years programming C and derivatives, I have simply internalized  
that zero is SUPPOSED to be false, dagnabit!

But I do think it would be worthwhile to allow classes to specify  
their own truth values, even if Ruby as shipped doesn’t take advantage  
of this feature.

-Mark

> **···**
>
> On Tue, Jul 15, 2003 at 06:17:46AM +0900, 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.

---

<div class="post-metadata">

**Author:** ![Ben\_Giddings](https://avatars.discourse-cdn.com/v4/letter/b/b19c9b/32.png) [@Ben\_Giddings](https://rubytalk.org/u/Ben_Giddings)\
**Post date:** [14 July 2003 22:17 UTC](https://rubytalk.org/t/booleans/6845/6 "2003-07-14T22:17:41Z")

</div>

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

> **···**
>
> 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.

---

<div class="post-metadata">

**Author:** ![Daniel\_Berger3](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@Daniel\_Berger3](https://rubytalk.org/u/Daniel_Berger3)\
**Post date:** [15 July 2003 03:14 UTC](https://rubytalk.org/t/booleans/6845/7 "2003-07-15T03:14:12Z")

</div>

“Mark J. Reed” [markjreed@mail.com](mailto:markjreed@mail.com) wrote in message news:[20030714214600.GB23895@mulan.thereeds.org](mailto:20030714214600.GB23895@mulan.thereeds.org)…

> **···**
>
> > On Tue, Jul 15, 2003 at 06:17:46AM +0900, 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.
> > 
> > Sure. Or just plain old != 0. But I always forget that I need to do  
> > anything at all beyond “if (expression returning Numeric)”. After  
> > 20 years programming C and derivatives, I have simply internalized  
> > that zero is SUPPOSED to be false, dagnabit!
> > 
> > But I do think it would be worthwhile to allow classes to specify  
> > their own truth values, even if Ruby as shipped doesn’t take advantage  
> > of this feature.
> > 
> > -Mark
> 
> That was part of the reason I submitted RCR #143 (on RubyGarden)  
> which, unfortunately, was rejected. Care to revive it?
> 
> Dan

---

<div class="post-metadata">

**Author:** ![robert\_ehteshamzadeh](https://avatars.discourse-cdn.com/v4/letter/r/e9bcb4/32.png) [@robert\_ehteshamzadeh](https://rubytalk.org/u/robert_ehteshamzadeh)\
**Post date:** [15 July 2003 03:34 UTC](https://rubytalk.org/t/booleans/6845/8 "2003-07-15T03:34:33Z")

</div>

> > I am constantly being bitten by the fact that 0 is true.

How about this : all non-nil objects are true, with the exception of  
instances of FalseClass.

The same issues come up in SQL, where not only is a numeric 0 non-nil,  
but so is zero-length string.

“Mark J. Reed” [markjreed@mail.com](mailto:markjreed@mail.com) wrote in message  
[news:20030714214600.GB23895@mulan.thereeds.org](mailto:news:20030714214600.GB23895@mulan.thereeds.org)…

> **···**
>
> > On Tue, Jul 15, 2003 at 06:17:46AM +0900, 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.
> > 
> > Sure. Or just plain old != 0. But I always forget that I need to do  
> > anything at all beyond “if (expression returning Numeric)”. After  
> > 20 years programming C and derivatives, I have simply internalized  
> > that zero is SUPPOSED to be false, dagnabit!
> > 
> > But I do think it would be worthwhile to allow classes to specify  
> > their own truth values, even if Ruby as shipped doesn’t take advantage  
> > of this feature.
> > 
> > -Mark

---

<div class="post-metadata">

**Author:** ![George2](https://avatars.discourse-cdn.com/v4/letter/g/8e7dd6/32.png) [@George2](https://rubytalk.org/u/George2)\
**Post date:** [15 July 2003 10:15 UTC](https://rubytalk.org/t/booleans/6845/9 "2003-07-15T10:15:34Z")

</div>

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

You can already override parts of Ruby to cause plenty of odd  
behaviour:

class Fixnum  
def + (y)  
1  
end   
end

puts “2 + 2 = #{2 + 2}”

I think Matz has commented on this topic before and said that the  
reason that only nil and false are logically false is for performance.  
The test for an object being nil or false is a very quick. If logical  
tests instead had to call .to\_b then that’d be an extra message send  
on every if, and, or, when, …

– George

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [15 July 2003 12:16 UTC](https://rubytalk.org/t/booleans/6845/10 "2003-07-15T12:16:22Z")

</div>

“George Marrows” [george.marrows@ps.ge.com](mailto:george.marrows@ps.ge.com) schrieb im Newsbeitrag  
[news:5e44d1a4.0307150201.7b1a6f08@posting.google.com](mailto:news:5e44d1a4.0307150201.7b1a6f08@posting.google.com)…

> > 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.
> 
> You can already override parts of Ruby to cause plenty of odd  
> behaviour:
> 
> class Fixnum  
> def + (y)  
> 1  
> end  
> end
> 
> puts “2 + 2 = #{2 + 2}”

That’s true, but why should we add another option to shoot ourselves in  
the foot? I regard the to\_b thing more serious than the changed operator

- that you present: logical values always have to do with the way an  
algorithm executes, while a different return value for + is still a number  
and will only have minor impacts in most cases. Especially think of  
someone changing 0.to\_b to return false and then using a module of someone  
else that relied on 0 being true…

> I think Matz has commented on this topic before and said that the  
> reason that only nil and false are logically false is for performance.  
> The test for an object being nil or false is a very quick. If logical  
> tests instead had to call .to\_b then that’d be an extra message send  
> on every if, and, or, when, …

That’s a serious reason, too.

Kind regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![Mark\_J\_Reed](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mark\_J\_Reed](https://rubytalk.org/u/Mark_J_Reed)\
**Post date:** [15 July 2003 12:16 UTC](https://rubytalk.org/t/booleans/6845/11 "2003-07-15T12:16:31Z")

</div>

> > > I am constantly being bitten by the fact that 0 is true.
> 
> How about this : all non-nil objects are true, with the exception of  
> instances of FalseClass.

Ok, see, I have absolutely no problem understanding the concept.  
Easy as pie. I know that 0 is true, and I know why it is true.  
But a couple decades of programming habits step in and write things  
(like “if n” where I intended “if n==0”) on my behalf long before  
my brain can step in and think “But what is Truth? Is Truth  
unchanging law?”

Most of my non-Ruby programming these days is in Java, and I do the  
same thing there, but at least the compiler catches it and yells at me.  
🙂

> The same issues come up in SQL, where not only is a numeric 0 non-nil,  
> but so is zero-length string.

Yeah, but that’s not the same issue. Since SQL is a declarative  
language, I have never once mistaken it for C :-), and I do not  
generally think of null database fields as equivalent to Boolean  
“false”. Instead they are an entirely different beast which requires  
things like NVL() (or COALESCE() or whatever your favorite RDBMS calls it);  
I put nulls in the same general category as outer joins and such:  
Ways To Deal With Data What Just Ain’t There.

Thanks, though.

-Mark

> **···**
>
> On Mon, Jul 14, 2003 at 08:23:48PM -0700, robert ehteshamzadeh wrote:

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [15 July 2003 12:25 UTC](https://rubytalk.org/t/booleans/6845/12 "2003-07-15T12:25:08Z")

</div>

> my brain can step in and think "But what is Truth? Is Truth  
> unchanging law?"

pigeon% perldoc perlfunc  
[...]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fcntl FILEHANDLE,FUNCTION,SCALAR  
[...]

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;You don't have to check for "defined" on the return from  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"fnctl". Like "ioctl", it maps a 0 return from the system call  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;into "0 but true" in Perl.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^^^^^^^^^^^^  
[...]  
pigeon%

Guy Decoux

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [15 July 2003 13:56 UTC](https://rubytalk.org/t/booleans/6845/13 "2003-07-15T13:56:55Z")

</div>

> Ok, see, I have absolutely no problem understanding the concept.  
> Easy as pie. I know that 0 is true, and I know why it is true.  
> But a couple decades of programming habits step in and write things  
> (like “if n” where I intended “if n==0”) on my behalf long before  
> my brain can step in and think “But what is Truth? Is Truth  
> unchanging law?”

Don’t forget to wash your hands… 😉

Hal

> **···**
>
> ----- Original Message -----  
> From: “Mark J. Reed” [markjreed@mail.com](mailto:markjreed@mail.com)  
> Newsgroups: comp.lang.ruby  
> To: “ruby-talk ML” [ruby-talk@ruby-lang.org](mailto:ruby-talk@ruby-lang.org)  
> Sent: Tuesday, July 15, 2003 7:16 AM  
> Subject: Re: Booleans

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [15 July 2003 12:57 UTC](https://rubytalk.org/t/booleans/6845/14 "2003-07-15T12:57:31Z")

</div>

Good point. Certain perl functions return “0E” when they mean “0”, so that  
it can be treated as ‘true’.

Just remember that Ruby isn’t Perl or C. It doesn’t take long to get used to  
it, and you’ll find it’s much cleaner. After all, why should the number “0”  
be treated differently from the number “1”?

Regards,

Brian.

> **···**
>
> On Tue, Jul 15, 2003 at 09:25:08PM +0900, ts wrote:
> 
> > > my brain can step in and think “But what is Truth? Is Truth  
> > > unchanging law?”
> > 
> > pigeon% perldoc perlfunc  
> > […]  
> > fcntl FILEHANDLE,FUNCTION,SCALAR  
> > […]
> > 
> > ```
> > You don't have to check for "defined" on the return from
> > "fnctl". Like "ioctl", it maps a 0 return from the system call
> > into "0 but true" in Perl.
> > ^^^^^^^^^^^^
> > 
> > ```
