Ruby – Booleans

In Ruby, there is actually no class called “Boolean” that instantiates true/false values. Instead, we have the following 2 classes:

  • TrueClass – which instantiates “true” values
  • FalseClass – which instantiates “false” values

Both of these classes are dervived from the “Object” class. This means that these true/false objects have access to all the methods that are defined in Object, e.g.:

puts true.class
puts false.class
puts nil.class


puts true.class.superclass
puts false.class.superclass
puts nil.class.superclass


puts true.to_s      # outputs the string "true"
puts false.nil?     # outputs false, since false itself is an 
                    # object that has instantiated from "FalseClass" class. 

This outputs:

PS C:\Temp\irb> ruby .\boolean.rb
TrueClass
FalseClass
NilClass
Object
Object
Object
true
false
PS C:\Temp\irb>