# Why don't TrueClass and FalseClass share a common Boolean ancestor

**URL:** <https://www.ruby-forum.com/t/why-dont-trueclass-and-falseclass-share-a-common-boolean-ancestor/227745>\
**Category:** Ruby\
**Created:** [March 27, 2013, 12:56pm UTC](https://www.ruby-forum.com/t/why-dont-trueclass-and-falseclass-share-a-common-boolean-ancestor/227745 "2013-03-27T12:56:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![reggieb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@reggieb](https://www.ruby-forum.com/u/reggieb)\
**Post date:** [March 27, 2013, 12:56pm UTC](https://www.ruby-forum.com/t/why-dont-trueclass-and-falseclass-share-a-common-boolean-ancestor/227745/1 "2013-03-27T12:56:20Z")

</div>

Both TrueClass and FalseClass inherit directly from the Object class.  
They share a common structure and do very similar jobs. So why don’t  
they share a common parent between them and Object? BooleanClass  
perhaps?

I wanted to test some input for true or false and thought  
true.class.ancestors and false.class.ancestors would give me a shared  
parent class to test for rather than have to check for each  
independently.

An academic question really, that piqued my curiosity and seemed more  
strange the deeper I looked. It got me looking into the C code behind  
Ruby for the first time (which can’t be a bad thing), where I was struck  
by the similarities between the two object structures.

From the positioning of the comments\*:

TrueClass methods appear to be defined by:

true\_to\_s, true\_and, true\_or, true\_xor.

FalseClass methods appear to be defined by:

false\_to\_s, false\_and, false\_or, false\_xor

Then there are some other methods that seem to be hung on the end and I  
assume from the names are associated with the core ruby space:

rb\_true, rb\_false, rb\_obj\_match, rb\_obj\_not\_match, rb\_obj\_cmp

Which look ripe to be placed within a BooleanClass name space to me.

Looking at [https://github.com/ruby/ruby/blob/trunk/object.c](https://github.com/ruby/ruby/blob/trunk/object.c) lines 1107  
to 1342.

So just wondered if anyone knew why there isn’t a dedicated parent class  
to TrueClass and FalseClass.

* * *

- My knowledge of C is non-existent, so please excuse any fundamental  
misunderstanding of the code that I may have made. For example, I’m  
failing to find where Qtrue and Qfalse are defined, and both seem to  
hold the key to understanding true and false within the ruby code.

---

<div class="post-metadata">

**Author:** ![reggieb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@reggieb](https://www.ruby-forum.com/u/reggieb)\
**Post date:** [March 27, 2013, 1:46pm UTC](https://www.ruby-forum.com/t/why-dont-trueclass-and-falseclass-share-a-common-boolean-ancestor/227745/2 "2013-03-27T13:46:38Z")

</div>

This can only be properly answered by a language designer, so here goes  
an  
improper conjecture :).

As you know all objects in Ruby are booleans. In this interpretation of  
the  
situation TrueClass is semantically no different than String: the true  
singleton and an empty string are as true objects as any other.  
Therefore  
it needs no extra hierarchy.

---

<div class="post-metadata">

**Author:** ![reggieb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@reggieb](https://www.ruby-forum.com/u/reggieb)\
**Post date:** [March 27, 2013, 1:53pm UTC](https://www.ruby-forum.com/t/why-dont-trueclass-and-falseclass-share-a-common-boolean-ancestor/227745/3 "2013-03-27T13:53:04Z")

</div>

Hi,

There’s nothing true and false commonly share, thus no Boolean class.  
Besides that, in Ruby, everything behave as Boolean value, as Xavier  
mentioned in [ruby-talk:406208].

```
          matz.

```

In message “Re: Why don’t TrueClass and FalseClass share a common  
Boolean ancestor”  
on Wed, 27 Mar 2013 20:56:24 +0900, “Rob N.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|Both TrueClass and FalseClass inherit directly from the Object class.  
|They share a common structure and do very similar jobs. So why don’t  
|they share a common parent between them and Object? BooleanClass  
|perhaps?  
|  
|I wanted to test some input for true or false and thought  
|true.class.ancestors and false.class.ancestors would give me a shared  
|parent class to test for rather than have to check for each  
|independently.  
|  
|An academic question really, that piqued my curiosity and seemed more  
|strange the deeper I looked. It got me looking into the C code behind  
|Ruby for the first time (which can’t be a bad thing), where I was struck  
|by the similarities between the two object structures.  
|  
|From the positioning of the comments\*:  
|  
|TrueClass methods appear to be defined by:  
|  
| true\_to\_s, true\_and, true\_or, true\_xor.  
|  
|FalseClass methods appear to be defined by:  
|  
| false\_to\_s, false\_and, false\_or, false\_xor  
|  
|Then there are some other methods that seem to be hung on the end and I  
|assume from the names are associated with the core ruby space:  
|  
| rb\_true, rb\_false, rb\_obj\_match, rb\_obj\_not\_match, rb\_obj\_cmp  
|  
|Which look ripe to be placed within a BooleanClass name space to me.  
|  
|Looking at [https://github.com/ruby/ruby/blob/trunk/object.c](https://github.com/ruby/ruby/blob/trunk/object.c) lines 1107  
|to 1342.  
|  
|So just wondered if anyone knew why there isn’t a dedicated parent class  
|to TrueClass and FalseClass.  
|  
|-------------------  
|\* My knowledge of C is non-existent, so please excuse any fundamental  
|misunderstanding of the code that I may have made. For example, I’m  
|failing to find where Qtrue and Qfalse are defined, and both seem to  
|hold the key to understanding true and false within the ruby code.  
|  
|–  
|Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![reggieb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@reggieb](https://www.ruby-forum.com/u/reggieb)\
**Post date:** [March 27, 2013, 1:57pm UTC](https://www.ruby-forum.com/t/why-dont-trueclass-and-falseclass-share-a-common-boolean-ancestor/227745/4 "2013-03-27T13:57:45Z")

</div>

On 27 March 2013 11:56, Rob N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Both TrueClass and FalseClass inherit directly from the Object class.  
> They share a common structure and do very similar jobs. So why don’t  
> they share a common parent between them and Object? BooleanClass  
> perhaps?

I’d be curious to know what exactly you’re testing, and why you need  
to deal with true and false as specific values.

Here’s my justification, although if anyone finds flaws, please point  
them out! (There might be better arguments than this.)

All objects that are not nil or false evaluate as truthy.

s = “foo”  
puts “You’ll see this.” if s

The presence of a Boolean starts to suggest code like this:

do\_something(arg) if arg.class == Boolean

This goes against duck typing. Let’s look at Integer, for instance.  
Integer wraps Fixnum and Bignum, but you _shouldn’t_ really ever do  
this:

process\_number(arg) if arg.is\_a?(Integer)

Instead you should allow objects to indicate they have Integer  
representations, without forcing them to subclass:

class BankBalance # Note this does not subclass Integer  
def initialize(amount)  
@amount = amount  
end

```
def to_int
  @amount
end

```

end

(First example off the top of my head. Might have problems, but treat  
as illustrative.)

Now any code that wants to deal with some notion of an integer can  
rely on BankBalance instances without any checks:

do\_something(number.to\_int)

BankBalance doesn’t implement an Integer so it shouldn’t subclass  
Integer, but by defining to\_int, the class is saying it can act like  
one in a certain way; the net result being that code can skip checking  
implementation details.

Note that to\_int is different from to\_i. to\_i is an explicit  
conversion to an integer, but to\_int is intended to give the message:  
“I act like an Integer.” Similarly: to\_str for acting like a String,  
distinct from explicit conversion with to\_s.

So with that in mind, you might imagine Boolean-ish duck typing based  
on something like to\_bool, right?

class TrueClass  
def to\_bool  
true  
end  
end

class FalseClass  
def to\_bool  
false  
end  
end

def NilClass  
def to\_bool  
false  
end  
end

But then (Basic)Object would need to define it so that all non-nil,  
non-false objects come back as truthy:

class Object  
def to\_bool  
true  
end  
end

But then you might hit a lot of confusion if something decides to  
define to\_bool as false: suddenly Ruby’s contract of “non-nil,  
non-false values are truthy” can have extra clauses added to it. And,  
what have you gained?

if arg.to\_bool  
# do stuff  
end

Ruby gives you this:

if arg  
# do stuff  
end

So I guess to summarise the above:

- Duck typing.
- It’s built in for you implicitly.

---

<div class="post-metadata">

**Author:** ![reggieb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@reggieb](https://www.ruby-forum.com/u/reggieb)\
**Post date:** [March 27, 2013, 5:56pm UTC](https://www.ruby-forum.com/t/why-dont-trueclass-and-falseclass-share-a-common-boolean-ancestor/227745/5 "2013-03-27T17:56:06Z")

</div>

Thank you for the responses.

So FalseClass is the special object as it behaves differently to other  
objects in boolean operations,

TrueClass is just a very simple object that in boolean operations  
behaves like other objects.

Looking at it on that level it makes sense not to have a common  
ancestor.

In response to Adam: For some background, I’m working on an gem I’ve  
created to simplify creating rules for arrays of objects. Up until now  
the logic has been simple, but I wanted to add some functions (sum,  
average, and count to start with).

So if I added a rule ‘sum(:some\_method) \> 4’, it would pass when  
some\_method was called on all the objects in the array, and the  
collected results added up to more than 4.

The code is here:

> <https://github.com/reggieb/array_logic/blob/master/lib/array_logic/rule.rb>

They we’re not failing very gracefully if the method being called did  
not return a number, and I was working on a way of catching that. The  
count function also worked with true and false, so I wanted to let them  
through too. So I added the numbers\_booleans\_or\_nils? method to handle  
the obvious problem input.

In hindsight, it makes more sense to refactor my array\_functions so that  
the commands being called can handle the problem input. It’s probably  
just  
needs a ‘collect(:to\_f)’ in front of the sum and average calls.

However, it’s the end of the day now, so the refactor will have to wait  
to the morning.

Thank you again for you responses. The more I use and delve into Ruby,  
the more I love it.
