Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Why doesn't Ruby have Boolean class?

Why doesn't Ruby have Boolean class?

Avatar for Masafumi Okura

Masafumi Okura

August 09, 2025
Tweet

More Decks by Masafumi Okura

Other Decks in Programming

Transcript

  1. introduce(self) • Name: OKURA Masafumiʢେ૔խ࢙ɺ͓͓͘Β·͞;Έʣ • From: Tokyo, Japan •

    Work: Freelancer • Activity: Kaigi on Rails (Founder, chief organizer), speaker (RubyConf Tw 2023/2025, EuRuKo 2023, Red Dot Ruby Conference 2024, Rails World 2025, Friendly.rb 2025), OSS (Alba and a few others) • Favorite method: `BasicObject#instance_eval` • Special note: I’m hosting a table in Code Party this evening!
  2. I’m going to talk about… 1. Fact: How Ruby implements

    Boolean-related classes and methods 2. Opinion: Why don’t we need Boolean class with Ruby
  3. The takeaways of this talk are… • The “Ruby Way”

    about Boolean • Know more about language • The design principle of programming languages • Decision of NOT adding Boolean class • (And How Matz thinks about this???) • (In the original talk, Matz wasn’t there)
  4. Boolean-related facts in Ruby • TrueClass and FalseClass are the

    class for true and false object, respectively • These classes are “singleton” classes that have only one instance, not having `new` method • TrueClass and FalseClass don’t have a common parent class • There is no method such as `to_bool`, but we can use `!!expr` idiom • Any object, not only true and false, can be used with `if` expression (false and `nil` are falsy, every other object including empty string and 0 are truthy)
  5. Other languages • There are “immediate values” (Java) • true

    and false are immediate values (not objects, no classes) in Java • In some languages, only `true` and `false` can be used for `if` statement (Java, Haskell, etc.) • In some languages, there are methods to convert objects into boolean values (Python, JavaScript, etc.)
  6. Rejected for several reasons: many gems and libraries had already

    introduced Boolean class. I don't want to break them. true and false are the only representative of true- false values. In Ruby. nil and false are falsy values, and everything else is a true value. There's no meaning for having a superclass of TrueClass and FalseClass as Boolean. Matz. ʢUnderscored by the speakerʣ
  7. 66

  8. 63

  9. Conclusion: true and false share nothing. They are the only

    representative, not having useful behaviors.
  10. > In object-oriented programming, a class defines the shared aspects

    of objects created from the class. https://en.wikipedia.org/wiki/Class_(computer_programming)
  11. Rejected for several reasons: many gems and libraries had already

    introduced Boolean class. I don't want to break them. true and false are the only representative of true- false values. In Ruby. nil and false are falsy values, and everything else is a true value. There's no meaning for having a superclass of TrueClass and FalseClass as Boolean. Matz. ʢUnderscored by the speakerʣ
  12. Rejected for several reasons: many gems and libraries had already

    introduced Boolean class. I don't want to break them. true and false are the only representative of true- false values. In Ruby. nil and false are falsy values, and everything else is a true value. There's no meaning for having a superclass of TrueClass and FalseClass as Boolean. Matz. ʢUnderscored by the speakerʣ
  13. Naming is hard • Recently added “Ractor” used to be

    “Guild” • Game developers complained that they already use “Guild” class • Recently added “Data” class was occupied by Ruby itself so it was safe • Adding “Boolean” may destroy other “Boolean” classes
  14. The reasons why we don’t need Boolean class • true

    and false share nothing • There’s no meaning of a superclass for them • Few use cases • `is_a?(Boolean) might be the only one • Compatibility issues • Naming is hard
  15. Lessons learned • Designing a programming language is interesting •

    In the issue, many developers express their thoughts and ideas • Not always “correct answer” • “This should be useful” might not be a right reason to add something • Ruby is different from other languages