safebool gem / library - 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

Gerald Bauer <[email protected]>
Newsgroups gmane.comp.lang.ruby.general
Message-ID <CAAxEZd_J=WACR+eNJtquRWpD3Bdxs9RADwMk3-Nhat0XD2k2bA@mail.gmail.com>
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

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.