# 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:** 20\
**Page:** 1

<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:** [March 30, 2006, 11:31pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/1 "2006-03-30T23:31:06Z")

</div>

Greetings!

I am wondering why there are two separate classes (TrueClass and  
FalseClass) to deal with boolean types insead of one class (something  
like Boolean?). Do you know  
why the library designer desided to use this approach?

Your help will be greatly apreciated.

Thanks

---

<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:** [March 30, 2006, 11:43pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/2 "2006-03-30T23:43:43Z")

</div>

On Mar 30, 2006, at 1:31 PM, PrimaryKey wrote:

> Greetings!
> 
> I am wondering why there are two separate classes (TrueClass and  
> FalseClass) to deal with boolean types insead of one class (something  
> like Boolean?). Do you know  
> why the library designer desided to use this approach?
> 
> Your help will be greatly apreciated.

This has been discussed many, many times before. Here’s a reading list:

[http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb?key=TrueClass](http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb?key=TrueClass)  
+FalseClass&cginame=namazu.rb&submit=Search&dbname=ruby-  
talk&max=50&whence=0

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<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:** [March 30, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/3 "2006-03-30T23:46:46Z")

</div>

Since everything in ruby is an object, there need to be objects for  
representing true and false. Once you have those, why do you need a  
Boolean class?

---

<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:** [March 31, 2006, 4:10pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/4 "2006-03-31T16:10:56Z")

</div>

PrimaryKey wrote:

> I am wondering why there are two separate classes (TrueClass and  
> FalseClass) to deal with boolean types insead of one class (something  
> like Boolean?). Do you know  
> why the library designer desided to use this approach?

I think it is because true and false don’t really have anything in  
common in Ruby. No way to share code.

You can always fix it pretty easily:

module Boolean; end  
[true, false].each do |obj|  
obj.extend(Boolean)  
end

true.is\_a?(Boolean) # =\> true  
false.is\_a?(Boolean) # =\> true

But note that in Ruby all objects (except false and nil) can be used as  
true values. And they are used for just that even in the standard  
library.

---

<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:** [March 31, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/5 "2006-03-31T17:15:14Z")

</div>

On Mar 31, 2006, at 9:27 AM, Christian N. wrote:

> case y  
> when Integer  
> when Boolean  
> end

case y  
when Integer  
when TrueClass,FalseClass  
end

works ok, right? I kind of wish the class names were just True and  
False.

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:** [March 31, 2006, 4:29pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/6 "2006-03-31T16:29:27Z")

</div>

“removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Since everything in ruby is an object, there need to be objects for  
> representing true and false. Once you have those, why do you need a  
> Boolean class?

if a.kind\_of? Boolean

case y  
when Integer  
when Boolean  
end

I often wanted this shortcut…

---

<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:** [March 31, 2006, 5:24pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/7 "2006-03-31T17:24:24Z")

</div>

On Fri, 31 Mar 2006, Christian N. wrote:

> when Boolean  
> end
> 
> I often wanted this shortcut…

indeed. i tend to use

case obj  
when Klass  
when TrueClass, FalseClass  
end

which is ugly.

another thing a real Boolean class could give is a ‘maybe’ obj such that

```
 maybe or true -> maybe
 maybe or true and false or true -> maybe
 maybe and false -> maybe

```

although i can’t think of anything attm to do with this it would  
undoubtably  
lead to some nice logical constructs that more closely parallel the way  
we  
think.

i’ve brought this up once or twice before - maybe we should just put  
together  
a patch and send to ruby-core?

regards.

-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:** [March 31, 2006, 5:39pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/8 "2006-03-31T17:39:47Z")

</div>

removed\_email\_address@domain.invalid wrote:

> On Fri, 31 Mar 2006, Christian N. wrote:
> 
> > if a.kind\_of? Boolean
> > 
> > case y  
> > when Integer  
> > when Boolean  
> > end

What kind of scenarios would you want to use such a construct? It seems  
to me not very rubyish to be switching based on the class of an object.  
As I understand it, the more conventional way to deal with potentially  
diverse argument types is to use the respond\_to? method. This keeps the  
door open for duck typing.

> another thing a real Boolean class could give is a ‘maybe’ obj such that  
> …  
> although i can’t think of anything attm to do with this it would undoubtably  
> lead to some nice logical constructs that more closely parallel the way we  
> think.

It sounds like you’re talking about a sort of restricted fuzzy logic,  
which is very cool, but is not appropriate for the core of a general  
purpose language. Considering that every expression can be interpreted  
in a boolean context, what would a maybe value do? Either behavior  
seems wrong.

---

<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:** [March 31, 2006, 7:27pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/9 "2006-03-31T19:27:01Z")

</div>

“removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> to me not very rubyish to be switching based on the class of an object.  
> As I understand it, the more conventional way to deal with potentially  
> diverse argument types is to use the respond\_to? method. This keeps the  
> door open for duck typing.

Except we don’t have a to\_bool, either ;-).

Still, I can saw enough cases where I wanted such a “case”, and  
grumbly used TrueClass,FalseClass.

---

<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:** [March 31, 2006, 6:19pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/10 "2006-03-31T18:19:42Z")

</div>

On Sat, 1 Apr 2006, removed\_email\_address@domain.invalid wrote:

> me not very rubyish to be switching based on the class of an object. As I  
> understand it, the more conventional way to deal with potentially diverse  
> argument types is to use the respond\_to? method. This keeps the door open  
> for duck typing.

i generally do use duck typing, but sometimes it is simply not  
appropriate or  
become too verbose. for example, consider a matrix class with the  
following  
behaviour

1d

```
 m[true] #=> all data returned
 m[] #=> all data returned
 m[true] = elems #=> data along all dims set
 m[] = elems #=> data along all dims set
 m[int] #=> treat as 1d array, return elem
 m[int] = elem #=> treat as 1d array, set idx to elem
 m[off, len] #=> treat as 1d array, return elems off thru 

```

len  
m[off, len] = elem #=\> treat as 1d array, set elems off thru len  
to elem  
m[a … b] #=\> treat as 1d array, return elems in range  
m[a … b] = elem #=\> treat as 1d array, set elems in range to  
elem  
m[n] #=\> idx one matrix by another

2d

```
 generalize all above so multiple indices access in 

```

multi-dimensional  
fashion. eg

```
   m[0,1,2] #= return idx x,y,z
   m[0,30..42,true] #= return column 0, rows 30 to 42, in all height 

```

dims

now, in case you are thinking that i’m just being difficult, this is  
exactly  
how the narray class works

[http://narray.rubyforge.org/SPEC.en](http://narray.rubyforge.org/SPEC.en)

have fun writing that with respond\_to? !! 😉

also, when working with concrete data structures (like binary output  
from C  
programs) it’s often quite sufficient to have a type mapping. further  
more  
it’s sometimes critical, for instace writing the word located at  
offset=42,  
len=4 with a packed int value or sending a given sequence of bytes down  
a  
socket that a given type is used and it’s much more natural to write

def send buf, which = nil  
case which  
when Fixnum  
socket.write [which].pack(‘N’)  
socket.write buf  
when NilClass  
socket.write [buf.size].pack(‘N’)  
socket.write buf  
when Range  
length = which.last - which.first  
socket.write [length].pack(‘N’)  
socket.write buf[which]  
else  
raise TypeError, which.class  
end  
end

than something using respond\_to?.

don’t get me wrong - i defintely advocate duck typing, but when the  
number of  
possible input types becomes large and behaviour is different depending  
on  
that type it becomes cumbersome. this is the price we pay for not  
having c–  
style polymorphism. in fact, it’s very common to see the two styles  
combined:

case arg  
when Fixnum  
…  
when Range  
…  
else # dunno - assume String and use in a duck type fashion  
end

> context, what would a maybe value do? Either behavior seems wrong.  
> i disagree - ruby (and hardware) already supports this style of logic in  
> several ways:

harp:~ \> irb  
irb(main):001:0\> nan = 0.0/0.0  
=\> NaN  
irb(main):002:0\> nan \* 42  
=\> NaN  
irb(main):003:0\> nan \* 42 + 42.0  
=\> NaN  
irb(main):004:0\> obj = Object.new and obj.taint  
=\> #Object:0xb75a5fac  
irb(main):005:0\> (obj.to\_s \<\< “string”).tainted?  
=\> true  
irb(main):006:0\> (“string” \<\< obj.to\_s).tainted?  
=\> true

there’s no good reason, imho, why this style of logical behaviour could  
not be  
part of the logical classes (TrueClass/FalseClass) and operators (and,  
or,  
not).

regards.

-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:** [March 31, 2006, 10:45pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/11 "2006-03-31T22:45:58Z")

</div>

On Sat, Apr 01, 2006 at 12:38:43AM +0900, removed\_email\_address@domain.invalid wrote:  
} removed\_email\_address@domain.invalid wrote:  
} \> On Fri, 31 Mar 2006, Christian N. wrote:  
} \> \> if a.kind\_of? Boolean  
} \> \>  
} \> \> case y  
} \> \> when Integer  
} \> \> when Boolean  
} \> \> end  
}  
} What kind of scenarios would you want to use such a construct? It  
seems  
} to me not very rubyish to be switching based on the class of an  
object.  
} As I understand it, the more conventional way to deal with potentially  
} diverse argument types is to use the respond\_to? method. This keeps  
the  
} door open for duck typing.  
[…]

The most obvious answer is that it is needed for various kinds of  
serialization. I wrote something like this just the other day. I needed  
to  
do XML-RPC serialization (yes, I know there is XML-RPC support in the  
standard library, but I needed something slightly different that was  
easier  
to do by hand), so I needed to know what kind of value I was  
serializing.

–Greg

---

<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:** [March 31, 2006, 9:10pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/12 "2006-03-31T21:10:06Z")

</div>

unknown wrote:

> ```
> maybe or true -> maybe
> 
> ```

Wouldn’t maybe or true be true?

–  
– Jim 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:** [March 31, 2006, 11:10pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/13 "2006-03-31T23:10:29Z")

</div>

removed\_email\_address@domain.invalid wrote:

> end  
> You could always assign some new constants to them:

True = TrueClass  
False = FalseClass

case y  
when Integer  
when True, False  
end

---

<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, 12:02am UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/14 "2006-04-01T00:02:24Z")

</div>

John W. Long wrote:

> You could always assign some new constants to them:
> 
> True = TrueClass  
> False = FalseClass
> 
> case y  
> when Integer  
> when True, False  
> end

Or just use the literal constants, that already exist:

case y  
when Integer  
when true, false  
end

---

<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:** [March 31, 2006, 10:58pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/15 "2006-03-31T22:58:09Z")

</div>

On Mar 31, 2006, at 10:21 AM, removed\_email\_address@domain.invalid wrote:

> maybe and false -\> maybe

Shouldn’t this be false?

{true, false} and false is going to be false regardless of what  
maybe is

---

<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, 12:45am UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/16 "2006-04-01T00:45:21Z")

</div>

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.

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:** [April 1, 2006, 1:21pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/17 "2006-04-01T13:21:32Z")

</div>

> another thing a real Boolean class could give is a ‘maybe’ obj such that
> 
> ```
> maybe or true -> maybe
> maybe or true and false or true -> maybe
> maybe and false -> maybe
> 
> ```

What whould this do

```
print "hello" if maybe

```

or this

```
while maybe
   print "hello"
end

```

// Niklas

---

<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, 10:36pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/18 "2006-04-01T22:36:23Z")

</div>

removed\_email\_address@domain.invalid wrote:

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

---

<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, 10:51pm UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/19 "2006-04-01T22:51:55Z")

</div>

On Sun, 2 Apr 2006, Joel VanderWerf wrote:

> doesn’t?

sheesh - can’t you tell it looks longer!

😉

-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:** [April 2, 2006, 1:46am UTC](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403/20 "2006-04-02T01:46:32Z")

</div>

On Apr 1, 2006, at 10:03 AM, removed\_email\_address@domain.invalid wrote:

> > 
> 
> number and look at the last bit for instance, 0 -\> false, 1 -\> true.
> 
> - h.h. the 14th dali lama

As if my code wasn’t already non-deterministic

[Next page](https://www.ruby-forum.com/t/trueclass-falseclass-vs-boolean/56403.md?page=2)
