# TrueClass/FalseClass vs. Boolean

**URL:** <https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403>\
**Category:** Ruby\
**Created:** [March 30, 2006, 11:31pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403 "2006-03-30T23:31:06Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![PrimaryKey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@PrimaryKey](https://www.ruby-forum.com/u/PrimaryKey)\
**Post date:** [April 1, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/21 "2006-04-01T17:05:15Z")

</div>

On Sat, 1 Apr 2006, Niklas Frykholm wrote:

> or this
> 
> while maybe  
> print “hello”  
> end

in a conditional it should evaluate to true/false radomly - compute a  
random  
number and look at the last bit for instance, 0 -\> false, 1 -\> true.

harp:~ \> cat a.rb  
def maybe() [rand].pack(‘f’).unpack(‘c’).first[0].zero? end

puts “hello” while maybe

harp:~ \> ruby a.rb  
hello  
hello

harp:~ \> ruby a.rb  
hello  
hello  
hello  
hello

harp:~ \> ruby a.rb  
hello

harp:~ \> ruby a.rb  
hello

cheers.

-a

---

<div class="post-metadata">

**Author:** ![PrimaryKey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@PrimaryKey](https://www.ruby-forum.com/u/PrimaryKey)\
**Post date:** [January 19, 2007, 9:20pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/22 "2007-01-19T21:20:57Z")

</div>

On 3/31/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Mar 31, 2006, at 5:00 PM, Florian F. 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.

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  
 → [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

---

<div class="post-metadata">

**Author:** ![PrimaryKey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@PrimaryKey](https://www.ruby-forum.com/u/PrimaryKey)\
**Post date:** [January 19, 2007, 10:05pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/23 "2007-01-19T22:05:03Z")

</div>

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  
> –\> [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?

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

---

<div class="post-metadata">

**Author:** ![PrimaryKey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b5ac83/32.png) [@PrimaryKey](https://www.ruby-forum.com/u/PrimaryKey)\
**Post date:** [January 19, 2007, 10:27pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/24 "2007-01-19T22:27:09Z")

</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://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403.md?page=1)
