Search code examples
rubylanguage-design

Why does Ruby have TrueClass and FalseClass instead of a single Boolean class?


I was working on serializing values when found out about this one. Ruby has a TrueClass class, and a FalseClass class, but it has no Boolean class. I'd like to know why is this.

I see some advantages in using a Boolean; for example, string parsing could be centralized on it.

Ruby developers are smarter than me, so there must be a lot of good reasons that I just don't see. But right now it looks to me like having OneClass and a TwoClass instead of Fixnum.


Solution

  • It seems that Matz himself answered this question on a mailing list message in 2004.

    Short version of his answer: "right now it works ok, adding a Boolean doesn't give any advantage".

    Personally I don't agree with that; the aforementioned "string parsing" is one example. Another one is that when you are applying different treatment to a variable depending on its type, (i.e. a yml parser) having a "Boolean" class is handy - it removes one "if". It also looks more correct, but that's a personal opinion.