Ruby does not have a class Boolean
. I noticed that boolean objects are of specific classes depending on the value, see:
true.class => TrueClass
false.class => FalseClass
Unlike other languages, such as C # and Java, where true
and false
are bool
, Ruby is TrueClass
or FalseClass
.
This implies: if I want to check if an object is Boolean, I have to do something like:
def boolean?(value)
[TrueClass, FalseClass].include? value.class
end
In this case, I see it as the only way to see if an object is of a boolean type. Otherwise, it would check only if an object is truthy or falsy.
!!nil #=> false
boolean?(nil) #=> false
!!"Olá!" #=> true
boolean?("Olá!") #=> false
!!false #=> false
boolean?(false) #=> true
!!true #=> true
boolean?(true) #=> true
Why was it so designed? What advantages does varying the type in question bring in the Ruby context?