Hello,
I've bundled up all the bool(ean) "hacks" in the safebool gem / library [1]
The new safe bool(ean) type adds Bool(), to_b, to_bool, bool?, false?, true?,
true.is_a?(Bool)==true, false.is_a?(Bool)==true, and more.
From the readme:
Why Bool in Ruby?
false.class #=> FalseClass
true.class #=> TrueClass
false.is_a?(Bool) #=> NameError: uninitialized constant Bool
true.is_a?(Bool) #=> NameError: uninitialized constant Bool
true.class.ancestors #=> [TrueClass, Object, Kernel, BasicObject]
false.class.ancestors #=> [TrueClass, Object, Kernel, BasicObject]
# -or-
"true".to_b #=> NoMethodError: undefined method `to_b' for String
1.to_b #=> NoMethodError: undefined method `to_b' for Integer
Bool("true") #=> NoMethodError: undefined method `Bool' for Kernel
Bool(1) #=> NoMethodError: undefined method `Bool' for Kernel
...
Why? Why not? Discuss.
require 'safebool'
false.is_a?(Bool) #=> true
true.is_a?(Bool) #=> true
true.class.ancestors #=> [TrueClass, Bool, Object, Kernel, BasicObject]
false.class.ancestors #=> [TrueClass, Bool, Object, Kernel, BasicObject]
# -or-
"true".to_b #=> true
1.to_b #=> true
Bool("true") #=> true
Bool(1) #=> true
"false".to_b #=> false
0.to_b #=> false
Bool("false") #=> false
Bool(0) #=> false
...
Happy coding with ruby. Cheers. Prost.
PS: Note: The safebool library / gem is part of the safe data
structure (safestruct) series [2].
[1] https://github.com/s6ruby/safebool
[2] https://github.com/s6ruby/safestruct