# TrueClass/FalseClass vs. Boolean

**URL:** <https://rubytalk.org/t/trueclass-falseclass-vs-boolean/26259>\
**Category:** ruby-talk\
**Created:** [30 March 2006 21:31 UTC](https://rubytalk.org/t/trueclass-falseclass-vs-boolean/26259 "2006-03-30T21:31:10Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [1 April 2006 20:49 UTC](https://rubytalk.org/t/trueclass-falseclass-vs-boolean/26259/21 "2006-04-01T20:49:15Z")

</div>

sheesh - can't you tell it looks longer!

😉

-a

> **···**
>
> On Sun, 2 Apr 2006, Joel VanderWerf wrote:
> 
> > ara.t.howard@noaa.gov wrote:
> > 
> > > &nbsp;&nbsp;def maybe() [rand].pack('f').unpack('c').first[0].zero? end
> > 
> > What does
> > 
> > [rand].pack('f').unpack('c').first[0]
> > 
> > do that
> > 
> > rand(2)
> > 
> > doesn't?
> 
> --  
> share your knowledge. it's a way to achieve immortality.  
> - h.h. the 14th dali lama

---

<div class="post-metadata">

**Author:** ![Ed\_Howland](https://avatars.discourse-cdn.com/v4/letter/e/7993a0/32.png) [@Ed\_Howland](https://rubytalk.org/u/Ed_Howland)\
**Post date:** [19 January 2007 20:20 UTC](https://rubytalk.org/t/trueclass-falseclass-vs-boolean/26259/22 "2007-01-19T20:20:32Z")

</div>

Another, related issue, is the lack of the \<=\> operator for either  
class. This comes in handy when sorting arrays of booleans or using  
sort\_by on a collection returned from activerecord that has a boolean  
attribute.

I know this has been covered in the archives of this list before, but  
I wasn't able to find any definitive answer from Matz or someone else.  
I thought it might be because in math Boolean is not really a scalar  
or a quantifiable value. Or in sortable collection terms, it doesn't  
implement Comparable. Other languages work with sorting on booleans  
because they might bind false to 0 and true to \> 0. So easily fixed by  
the to\_i above.

But in terms of Ruby "doing the right thing", you'd expect  
[false, true, false].sort  
&nbsp;&nbsp;&nbsp;&nbsp;--\> [false, false, true]  
because it does this just about everywhere else. This is such a common  
idiom in CS, that I think Ruby should implement it. But then, I only  
have just the 2 cents. Anyone know the main reason it does not?

Ed

> **···**
>
> On 3/31/06, gwtmp01@mac.com \<gwtmp01@mac.com\> wrote:
> 
> > On Mar 31, 2006, at 5:00 PM, Florian Frank wrote:  
> > \> Or just use the literal constants, that already exist:  
> > \> case y  
> > \> when Integer  
> > \> when true, false  
> > \> end
> > 
> > Another example where the Ruby 'does the right thing'.  
> > Thanks for the reminder.
> 
> --  
> Ed Howland
> 
> > **[Green Programming](http://greenprogrammer.blogspot.com)**
> >
> > Environmentally safe code

---

<div class="post-metadata">

**Author:** ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)\
**Post date:** [19 January 2007 21:04 UTC](https://rubytalk.org/t/trueclass-falseclass-vs-boolean/26259/23 "2007-01-19T21:04:47Z")

</div>

interesting observation

Since true and false are not instances of the same class it is  
like asking how to compare 4 and :foobar or 6.32 and "tuesday".

One possibility would be to have \<=\> be defined in terms of object\_id  
by default similar to how == is defined. Since false.object\_id is 0  
and true.object\_id is 2 that does give you [false,false,true]  
in your example but for an obscure, implementation dependent reason.

Gary Wright

> **···**
>
> On Jan 19, 2007, at 3:20 PM, Ed Howland wrote:
> 
> > But in terms of Ruby "doing the right thing", you'd expect  
> > [false, true, false].sort  
> > &nbsp;&nbsp;&nbsp;--\> [false, false, true]  
> > because it does this just about everywhere else. This is such a common  
> > idiom in CS, that I think Ruby should implement it. But then, I only  
> > have just the 2 cents. Anyone know the main reason it does not?

---

<div class="post-metadata">

**Author:** ![Collins\_Justin](https://avatars.discourse-cdn.com/v4/letter/c/e19adc/32.png) [@Collins\_Justin](https://rubytalk.org/u/Collins_Justin)\
**Post date:** [19 January 2007 21:26 UTC](https://rubytalk.org/t/trueclass-falseclass-vs-boolean/26259/24 "2007-01-19T21:26:08Z")

</div>

Ed Howland wrote:

> Another, related issue, is the lack of the \<=\> operator for either  
> class. This comes in handy when sorting arrays of booleans or using  
> sort\_by on a collection returned from activerecord that has a boolean  
> attribute.
> 
> I know this has been covered in the archives of this list before, but  
> I wasn't able to find any definitive answer from Matz or someone else.

The reason, IIRC, was the lack of meaning for true \> false, false \> true, etc.

-Justin

[Previous page](https://rubytalk.org/t/trueclass-falseclass-vs-boolean/26259.md?page=1)
