safebool 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +66 -7
- data/lib/safebool.rb +7 -3
- data/lib/safebool/version.rb +1 -1
- data/test/test_bool.rb +26 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b61c7f7d5196f735be3c79898e36e5fc018828ac
|
4
|
+
data.tar.gz: 99ea5034038bae80ccd19c7fa4bdd113673c124d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12fc6db01d0dab4916a22ed118aeace91b32be276a4e9c69ac4a4857b8a36a054d28705d970a9787b2567d3cda0a8f4e80cd5393ee0a1c1742fa6a00eb5e44b7
|
7
|
+
data.tar.gz: 19968f82c51794c0954a6b75b82f3417d0876d20695cef4686d0063c9b85835fd1d9972f5da07d739b47b5534c91a19abc13c0c505da48c64a9059a82e817add
|
data/README.md
CHANGED
@@ -47,8 +47,55 @@ Bool(1) #=> NoMethodError: undefined method `Bool' for Kernel
|
|
47
47
|
...
|
48
48
|
```
|
49
49
|
|
50
|
+
|
51
|
+
**Everything is `true` except `false` and `nil`.**
|
52
|
+
Just use the bang bang (`!!`) doubled-up (logical) boolean not operator for `to_b` conversion:
|
53
|
+
|
54
|
+
``` ruby
|
55
|
+
!! false #=> false
|
56
|
+
!! nil #=> false
|
57
|
+
|
58
|
+
!! true #=> true
|
59
|
+
!! "false" #=> true
|
60
|
+
!! "" #=> true
|
61
|
+
!! 0 #=> true
|
62
|
+
!! 1 #=> true
|
63
|
+
!! [] #=> true
|
64
|
+
!! {} #=> true
|
65
|
+
!! 0.0 #=> true
|
66
|
+
!! :false #=> true
|
67
|
+
# ...
|
68
|
+
```
|
69
|
+
|
50
70
|
Why? Why not? Discuss.
|
51
71
|
|
72
|
+
## Intro
|
73
|
+
|
74
|
+
### What's a Bool?
|
75
|
+
|
76
|
+
In computer science, the Boolean data type is a data type that has one of two possible values (usually denoted _true_ and _false_),
|
77
|
+
intended to represent the two truth values of logic and Boolean algebra.
|
78
|
+
It is named after George Boole, who first defined an algebraic system of logic in the mid 19th century.
|
79
|
+
|
80
|
+
(Source: [Boolean data type @ Wikipedia](https://en.wikipedia.org/wiki/Boolean_data_type))
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
### Trivia Quiz - The States of Bool
|
85
|
+
|
86
|
+
Q: How many states has a boolean type in a (classic) programming language?
|
87
|
+
|
88
|
+
- [ A ] 1 - One State
|
89
|
+
- [ B ] 2 - Two States
|
90
|
+
- [ C ] 3 - Three States
|
91
|
+
- [ D ] Other. Please tell
|
92
|
+
|
93
|
+
A: In practice¹ three really :-), that is, `true`, `false` and
|
94
|
+
undefined (e.g. `nil`).
|
95
|
+
|
96
|
+
1: with nil-able / null-able types
|
97
|
+
|
98
|
+
|
52
99
|
|
53
100
|
|
54
101
|
## Usage
|
@@ -87,14 +134,14 @@ Bool(1) #=> true
|
|
87
134
|
|
88
135
|
How about handling errors on invalid bool values when converting / parsing?
|
89
136
|
|
90
|
-
1. `to_b` always returns a bool even if the conversion / parsing fails e.g. `true`
|
137
|
+
1. `to_b` always returns a bool even if the conversion / parsing fails e.g. `true` for invalid numbers or strings and `false` (for empty / blank strings) on error
|
91
138
|
2. `parse_bool / to_bool` always returns `nil` if the conversion / parsing fails
|
92
139
|
3. `Bool()` always raises an `ArgumentError` if the conversion / parsing fails
|
93
140
|
and a `TypeError` if the conversion is unsupported (e.g. expected required `parse_bool` method missing / undefined)
|
94
141
|
|
95
142
|
|
96
143
|
``` ruby
|
97
|
-
"2".to_b #=>
|
144
|
+
"2".to_b #=> true
|
98
145
|
"2".to_bool #=> nil
|
99
146
|
"2".to_bool.bool? #=> false
|
100
147
|
"2".to_bool.is_a?(Bool) #=> false
|
@@ -105,6 +152,17 @@ Bool("2") #=> ArgumentError: invalid value "2":String for Bool();
|
|
105
152
|
2.to_bool.bool? #=> false
|
106
153
|
2.to_bool.is_a?(Bool) #=> false
|
107
154
|
Bool(2) #=> ArgumentError: invalid value 2:Integer for Bool(); parse_bool failed (returns nil)
|
155
|
+
|
156
|
+
"".to_b #=> false
|
157
|
+
"".to_bool #=> nil
|
158
|
+
"".to_bool.bool? #=> false
|
159
|
+
"".to_bool.is_a?(Bool) #=> false
|
160
|
+
Bool("") #=> ArgumentError: invalid value "":String for Bool(); parse_bool failed (returns nil)
|
161
|
+
|
162
|
+
# note: same for "blank" strings
|
163
|
+
" ".to_b #=> false
|
164
|
+
" ".to_bool #=> nil
|
165
|
+
|
108
166
|
...
|
109
167
|
```
|
110
168
|
|
@@ -114,7 +172,7 @@ Bool(2) #=> ArgumentError: invalid value 2:Integer for Bool(); p
|
|
114
172
|
- Returns `true` if string is one of these values: **t**, **true**, **on**, **y**, **yes**, **1**.
|
115
173
|
- Returns `false` if string is one of these values: **f**, **false**, **off**, **n**, **no**, **0**.
|
116
174
|
|
117
|
-
For invalid boolean string values `to_b` returns `
|
175
|
+
For invalid boolean string values `to_b` returns `true` by default except for empty / blank strings where `to_b` returns `false`.
|
118
176
|
See the "Handling Errors" section for more options.
|
119
177
|
|
120
178
|
Note: The `Bool.parse` method ignores leading and trailing spaces and upper and lower cases e.g. ` FaLSe ` is the same as `false`.
|
@@ -170,8 +228,9 @@ Note: The `Bool.parse` method ignores leading and trailing spaces and upper and
|
|
170
228
|
|
171
229
|
''.to_b #=> false
|
172
230
|
' '.to_b #=> false
|
173
|
-
|
174
|
-
'
|
231
|
+
|
232
|
+
'xxx'.to_b #=> true
|
233
|
+
'bool'.to_b #=> true
|
175
234
|
|
176
235
|
''.to_bool #=> nil
|
177
236
|
' '.to_bool #=> nil
|
@@ -198,8 +257,8 @@ Same as `self.to_s.to_b` or `self.to_s.to_bool`.
|
|
198
257
|
:n.to_b #=> false
|
199
258
|
:no.to_b #=> false
|
200
259
|
|
201
|
-
:xxx.to_b #=>
|
202
|
-
:bool.to_b #=>
|
260
|
+
:xxx.to_b #=> true
|
261
|
+
:bool.to_b #=> true
|
203
262
|
|
204
263
|
:xxx.to_bool #=> nil
|
205
264
|
:bool.to_bool #=> nil
|
data/lib/safebool.rb
CHANGED
@@ -140,9 +140,13 @@ end
|
|
140
140
|
|
141
141
|
class String
|
142
142
|
def to_b
|
143
|
-
|
144
|
-
|
145
|
-
|
143
|
+
if strip.empty? ## add special case for empty / blank strings - return false (!)
|
144
|
+
value = false
|
145
|
+
else
|
146
|
+
value = parse_bool()
|
147
|
+
value = true if value.nil? ## note: return true for all undefined / unknown string values that cannot convert to bool (except empty string)
|
148
|
+
value
|
149
|
+
end
|
146
150
|
end
|
147
151
|
def parse_bool() Bool.parse( self ); end
|
148
152
|
end
|
data/lib/safebool/version.rb
CHANGED
data/test/test_bool.rb
CHANGED
@@ -36,6 +36,20 @@ class TestBool < MiniTest::Test
|
|
36
36
|
|
37
37
|
assert_equal true, false.frozen? ## (always) true by default
|
38
38
|
assert_equal true, true.frozen? ## (always) true by default
|
39
|
+
|
40
|
+
|
41
|
+
assert_equal false, !! false
|
42
|
+
assert_equal false, !! nil
|
43
|
+
|
44
|
+
assert_equal true, !! true
|
45
|
+
assert_equal true, !! "false"
|
46
|
+
assert_equal true, !! ""
|
47
|
+
assert_equal true, !! 0
|
48
|
+
assert_equal true, !! 1
|
49
|
+
assert_equal true, !! []
|
50
|
+
assert_equal true, !! {}
|
51
|
+
assert_equal true, !! 0.0
|
52
|
+
assert_equal true, !! :false
|
39
53
|
end
|
40
54
|
|
41
55
|
def test_parse
|
@@ -88,6 +102,12 @@ class TestBool < MiniTest::Test
|
|
88
102
|
assert_equal true, Bool("y")
|
89
103
|
assert_equal true, Bool("1")
|
90
104
|
|
105
|
+
|
106
|
+
assert_equal false, "".to_b
|
107
|
+
assert_equal false, " ".to_b
|
108
|
+
assert_equal true, "2".to_b
|
109
|
+
assert_equal true, "xxx".to_b
|
110
|
+
|
91
111
|
assert_equal true, "".to_bool == nil
|
92
112
|
assert_equal true, " ".to_bool == nil
|
93
113
|
assert_equal true, "2".to_bool == nil
|
@@ -123,8 +143,12 @@ class TestBool < MiniTest::Test
|
|
123
143
|
assert_equal true, Bool(:y)
|
124
144
|
assert_equal true, Bool(:"1")
|
125
145
|
|
126
|
-
assert_equal
|
127
|
-
assert_equal true,
|
146
|
+
assert_equal false, :" ".to_b
|
147
|
+
assert_equal true, :"2".to_b
|
148
|
+
assert_equal true, :"xxx".to_b
|
149
|
+
|
150
|
+
assert_equal true, :'2'.to_bool == nil
|
151
|
+
assert_equal true, :xxx.to_bool == nil
|
128
152
|
|
129
153
|
assert_equal false, :'2'.to_bool.bool?
|
130
154
|
assert_equal false, :'xxx'.to_bool.bool?
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: safebool
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gerald Bauer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-04-
|
11
|
+
date: 2019-04-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rdoc
|