safebool 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Manifest.txt +2 -0
- data/README.md +76 -2
- data/lib/safebool.rb +102 -0
- data/lib/safebool/version.rb +2 -2
- data/test/helper.rb +8 -0
- data/test/test_bool.rb +96 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 49890c47adb87fba1069508825a740bd74ebbf3f
|
4
|
+
data.tar.gz: b29817d48444282a920610adc1ab79f56a3f926b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 83a6120727830d700d61e9b79933f372d353767f533b8b2ae5562c75996702e9871ab844f19dacd25faacbe8827dae6bc65879e6e2a2306379cabe32c9f2332d
|
7
|
+
data.tar.gz: 236b0f27a9ddf40a7b7ae8ee77b0596c8e1a323e919c1112be22ab3a093ad15cce268596bababd9936d1807158b2eff547d0ce8d50c5c60c9a2276a0b2ead323
|
data/Manifest.txt
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
|
2
|
-
# Safe Bool Type - `Bool()`, `to_b`, `to_bool`,
|
2
|
+
# Safe Bool Type - `Bool()`, `to_b`, `to_bool`, and More
|
3
3
|
|
4
4
|
|
5
5
|
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
|
@@ -11,9 +11,83 @@ safebool gem / library - safe bool(ean) type adds `Bool()`, `to_b`, `to_bool`, `
|
|
11
11
|
* rdoc :: [rubydoc.info/gems/safebool](http://rubydoc.info/gems/safebool)
|
12
12
|
|
13
13
|
|
14
|
+
|
15
|
+
## Why `Bool` in Ruby?
|
16
|
+
|
17
|
+
|
18
|
+
``` ruby
|
19
|
+
false.class #=> FalseClass
|
20
|
+
true.class #=> TrueClass
|
21
|
+
false.is_a?(Bool) #=> NameError: uninitialized constant Bool
|
22
|
+
true.is_a?(Bool) #=> NameError: uninitialized constant Bool
|
23
|
+
true.class.ancestors #=> [TrueClass, Object, Kernel, BasicObject]
|
24
|
+
false.class.ancestors #=> [TrueClass, Object, Kernel, BasicObject]
|
25
|
+
|
26
|
+
# -or-
|
27
|
+
|
28
|
+
"true".to_b #=> NoMethodError: undefined method `to_b' for String
|
29
|
+
1.to_b #=> NoMethodError: undefined method `to_b' for Integer
|
30
|
+
Bool("true") #=> NoMethodError: undefined method `Bool' for Kernel
|
31
|
+
Bool(1) #=> NoMethodError: undefined method `Bool' for Kernel
|
32
|
+
...
|
33
|
+
```
|
34
|
+
|
35
|
+
Why? Why not? Discuss.
|
36
|
+
|
37
|
+
|
38
|
+
|
14
39
|
## Usage
|
15
40
|
|
16
|
-
|
41
|
+
``` ruby
|
42
|
+
false.is_a?(Bool) #=> true
|
43
|
+
true.is_a?(Bool) #=> true
|
44
|
+
true.class.ancestors #=> [TrueClass, Bool, Object, Kernel, BasicObject]
|
45
|
+
false.class.ancestors #=> [TrueClass, Bool, Object, Kernel, BasicObject]
|
46
|
+
|
47
|
+
# -or-
|
48
|
+
|
49
|
+
"true".to_b #=> true
|
50
|
+
1.to_b #=> true
|
51
|
+
Bool("true") #=> true
|
52
|
+
Bool(1) #=> true
|
53
|
+
|
54
|
+
"false".to_b #=> false
|
55
|
+
0.to_b #=> false
|
56
|
+
Bool("false") #=> false
|
57
|
+
Bool(0) #=> false
|
58
|
+
|
59
|
+
...
|
60
|
+
```
|
61
|
+
|
62
|
+
|
63
|
+
More methods added to `Kernel` include `bool?`, `false?`, `true?`.
|
64
|
+
Example:
|
65
|
+
|
66
|
+
|
67
|
+
``` ruby
|
68
|
+
# bool? - returns true if object class is TrueClass or FalseClass, otherwise false
|
69
|
+
|
70
|
+
true.bool? #=> true
|
71
|
+
false.bool? #=> true
|
72
|
+
nil.bool? #=> false
|
73
|
+
|
74
|
+
# false? - returns true if object class is FalseClass, otherwise false
|
75
|
+
|
76
|
+
false.false? #=> true
|
77
|
+
true.false? #=> false
|
78
|
+
nil.false? #=> false
|
79
|
+
|
80
|
+
# true? - returns true is object class is TrueClass, otherwise false
|
81
|
+
|
82
|
+
true.true? #=> true
|
83
|
+
false.true? #=> false
|
84
|
+
nil.true? #=> false
|
85
|
+
```
|
86
|
+
|
87
|
+
And some more.
|
88
|
+
See the [`safebool.rb`](https://github.com/s6ruby/safebool/blob/master/lib/safebool.rb) source.
|
89
|
+
|
90
|
+
|
17
91
|
|
18
92
|
|
19
93
|
## License
|
data/lib/safebool.rb
CHANGED
@@ -6,5 +6,107 @@ require 'pp'
|
|
6
6
|
require 'safebool/version' # note: let version always go first
|
7
7
|
|
8
8
|
|
9
|
+
module Bool
|
10
|
+
def self.zero() false; end ## note: false.frozen? == true by default
|
11
|
+
|
12
|
+
def to_b() self; end
|
13
|
+
def to_bool() self; end
|
14
|
+
end # module Bool
|
15
|
+
|
16
|
+
|
17
|
+
class FalseClass
|
18
|
+
include Bool ## "hack" - enables false.is_a?(Bool)
|
19
|
+
|
20
|
+
def zero?() true; end
|
21
|
+
end
|
22
|
+
|
23
|
+
class TrueClass
|
24
|
+
include Bool ## "hack" - enables true.is_a?(Bool)
|
25
|
+
|
26
|
+
def zero?() false; end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
module Kernel
|
35
|
+
|
36
|
+
######
|
37
|
+
## Returns true if object class is TrueClass or FalseClass, otherwise false.
|
38
|
+
##
|
39
|
+
## true.bool? #=> true
|
40
|
+
## false.bool? #=> true
|
41
|
+
## nil.bool? #=> false
|
42
|
+
def bool?() self.class == TrueClass || self.class == FalseClass; end ## todo/discuss: use "shortcut" self == true || self == false - why? why not?
|
43
|
+
|
44
|
+
#########
|
45
|
+
## Returns true if object class is FalseClass, otherwise false.
|
46
|
+
##
|
47
|
+
## false.false? #=> true
|
48
|
+
## true.false? #=> false
|
49
|
+
## nil.false? #=> false
|
50
|
+
def false?() self.class == FalseClass; end ## todo/discuss: use "shortcut" self == false - why? why not?
|
51
|
+
|
52
|
+
############
|
53
|
+
## Returns true is object class is TrueClass, otherwise false.
|
54
|
+
##
|
55
|
+
## true.true? #=> true
|
56
|
+
## false.true? #=> false
|
57
|
+
## nil.true? #=> false
|
58
|
+
def true?() self.class == TrueClass; end ## todo/discuss: use "shortcut" self == true - why? why not?
|
59
|
+
|
60
|
+
|
61
|
+
#####
|
62
|
+
# default "explicit" conversion to bool for all objects
|
63
|
+
def to_b() self ? true : false; end
|
64
|
+
|
65
|
+
|
66
|
+
### "global" conversion function / method
|
67
|
+
def Bool( o )
|
68
|
+
## todo/discuss: check convention
|
69
|
+
## only (explicit) convert if to_bool present - why? why not?
|
70
|
+
## - check to_int/to_str/etc.
|
71
|
+
if o.respond_to?( :to_bool )
|
72
|
+
o.to_bool
|
73
|
+
elsif o.respond_to?( :to_b )
|
74
|
+
o.to_b
|
75
|
+
else
|
76
|
+
raise TypeError.new( "cannot convert >#{o.class.inspect}< to Bool; method to_bool or to_b expected")
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
class String
|
85
|
+
def to_b
|
86
|
+
case self.downcase.strip
|
87
|
+
when 'true', 'yes', 'on', 't', 'y', '1'
|
88
|
+
return true
|
89
|
+
## when 'nil', 'null' # todo/discuss - add why? why not?
|
90
|
+
## return nil
|
91
|
+
else
|
92
|
+
return false
|
93
|
+
end
|
94
|
+
end
|
95
|
+
alias_method :to_bool, :to_b
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
class Symbol
|
100
|
+
def to_b() to_s.to_b; end
|
101
|
+
alias_method :to_bool, :to_b
|
102
|
+
end
|
103
|
+
|
104
|
+
class Numeric
|
105
|
+
def to_b() self == 0 ? false : true; end
|
106
|
+
alias_method :to_bool, :to_b
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
|
9
111
|
|
10
112
|
puts SaferBool.banner ## say hello
|
data/lib/safebool/version.rb
CHANGED
data/test/helper.rb
ADDED
data/test/test_bool.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
###
|
4
|
+
# to run use
|
5
|
+
# ruby -I ./lib -I ./test test/test_bool.rb
|
6
|
+
|
7
|
+
|
8
|
+
require 'helper'
|
9
|
+
|
10
|
+
|
11
|
+
class TestBool < MiniTest::Test
|
12
|
+
|
13
|
+
def test_bool
|
14
|
+
assert_equal true, false.is_a?(Bool)
|
15
|
+
assert_equal true, false.false?
|
16
|
+
assert_equal false, false.true?
|
17
|
+
assert_equal true, false.bool?
|
18
|
+
assert_equal false, false.to_b
|
19
|
+
|
20
|
+
assert_equal true, true.is_a?(Bool)
|
21
|
+
assert_equal true, true.true?
|
22
|
+
assert_equal false, true.false?
|
23
|
+
assert_equal true, true.bool?
|
24
|
+
assert_equal true, true.to_b
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_nil
|
28
|
+
assert_equal false, nil.to_b
|
29
|
+
assert_equal false, Bool(nil)
|
30
|
+
|
31
|
+
assert_equal false, nil.false?
|
32
|
+
assert_equal false, nil.true?
|
33
|
+
assert_equal false, nil.bool?
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_string
|
37
|
+
assert_equal false, "true".false?
|
38
|
+
assert_equal false, "true".true?
|
39
|
+
assert_equal false, "true".bool?
|
40
|
+
|
41
|
+
assert_equal true, "true".to_b
|
42
|
+
assert_equal true, "yes".to_b
|
43
|
+
assert_equal true, "on".to_b
|
44
|
+
assert_equal true, "t".to_b
|
45
|
+
assert_equal true, "y".to_b
|
46
|
+
assert_equal true, "1".to_b
|
47
|
+
|
48
|
+
assert_equal true, "TRUE".to_b
|
49
|
+
assert_equal true, "YES".to_b
|
50
|
+
assert_equal true, "ON".to_b
|
51
|
+
assert_equal true, "T".to_b
|
52
|
+
assert_equal true, "Y".to_b
|
53
|
+
|
54
|
+
assert_equal true, Bool("true")
|
55
|
+
assert_equal true, Bool("yes")
|
56
|
+
assert_equal true, Bool("on")
|
57
|
+
assert_equal true, Bool("t")
|
58
|
+
assert_equal true, Bool("y")
|
59
|
+
assert_equal true, Bool("1")
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_symbol
|
63
|
+
assert_equal false, :true.false?
|
64
|
+
assert_equal false, :true.true?
|
65
|
+
assert_equal false, :true.bool?
|
66
|
+
|
67
|
+
assert_equal true, :true.to_b
|
68
|
+
assert_equal true, :yes.to_b
|
69
|
+
assert_equal true, :on.to_b
|
70
|
+
assert_equal true, :t.to_b
|
71
|
+
assert_equal true, :y.to_b
|
72
|
+
assert_equal true, :"1".to_b
|
73
|
+
|
74
|
+
assert_equal true, Bool(:true)
|
75
|
+
assert_equal true, Bool(:yes)
|
76
|
+
assert_equal true, Bool(:on)
|
77
|
+
assert_equal true, Bool(:t)
|
78
|
+
assert_equal true, Bool(:y)
|
79
|
+
assert_equal true, Bool(:"1")
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
def test_integer
|
84
|
+
assert_equal false, 1.false?
|
85
|
+
assert_equal false, 1.true?
|
86
|
+
assert_equal false, 1.bool?
|
87
|
+
|
88
|
+
assert_equal false, 0.to_b
|
89
|
+
assert_equal true, 1.to_b
|
90
|
+
assert_equal true, 2.to_b
|
91
|
+
|
92
|
+
assert_equal false, Bool(0)
|
93
|
+
assert_equal true, Bool(1)
|
94
|
+
assert_equal true, Bool(2)
|
95
|
+
end
|
96
|
+
end # class TestBool
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: safebool
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gerald Bauer
|
@@ -56,6 +56,8 @@ files:
|
|
56
56
|
- Rakefile
|
57
57
|
- lib/safebool.rb
|
58
58
|
- lib/safebool/version.rb
|
59
|
+
- test/helper.rb
|
60
|
+
- test/test_bool.rb
|
59
61
|
homepage: https://github.com/s6ruby/safebool
|
60
62
|
licenses:
|
61
63
|
- Public Domain
|