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