safebool 1.0.0 → 1.1.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 +169 -14
- data/Rakefile +1 -1
- data/lib/safebool.rb +48 -28
- data/lib/safebool/version.rb +1 -1
- data/test/test_bool.rb +94 -24
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3259e4df2ebfb2f5359249c7d1fc87dbb03c23d3
|
4
|
+
data.tar.gz: 8357baa33140416175df3b652581a30a381e0d9e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12332af592e5acda2b25cfc6919c281c304fa0ed82f3804dab36736c32476e74d9cbc2c5d7f807b111f2fbc72cd210fa6858a0feb5ff2ef3b051aa7c633bfb68
|
7
|
+
data.tar.gz: ff2e864c3b041f56b78be5ef1a1342984487affd5d56b8ef440c495fd69889b4a75c7bdb701e9980c36d49c5b9481747a5eec25c54338d5666ad20c5b1438705
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
# Safe Bool Type - `Bool()`, `to_b`, `to_bool`, and More
|
3
3
|
|
4
4
|
|
5
|
-
safebool gem / library - safe bool
|
5
|
+
safebool gem / library - safe bool / boolean type adds `Bool()`, `to_b`, `parse_bool` / `to_bool`, `bool?`, `false?`, `true?`, `true.is_a?(Bool)==true`, `false.is_a?(Bool)==true`, and more
|
6
6
|
|
7
7
|
|
8
8
|
* home :: [github.com/s6ruby/safebool](https://github.com/s6ruby/safebool)
|
@@ -25,10 +25,25 @@ false.class.ancestors #=> [FalseClass, Object, Kernel, BasicObject]
|
|
25
25
|
|
26
26
|
# -or-
|
27
27
|
|
28
|
+
false.to_s #=> "false"
|
29
|
+
true.to_s #=> "true"
|
30
|
+
false.to_i #=> NoMethodError: undefined method `to_i' for false:FalseClass
|
31
|
+
true.to_i #=> NoMethodError: undefined method `to_i' for true:TrueClass
|
32
|
+
Integer(false) #=> TypeError: can't convert false into Integer
|
33
|
+
Integer(true) #=> TypeError: can't convert true into Integer
|
34
|
+
|
35
|
+
# -or-
|
36
|
+
|
37
|
+
"false".to_b #=> NoMethodError: undefined method `to_b' for String
|
38
|
+
0.to_b #=> NoMethodError: undefined method `to_b' for Integer
|
39
|
+
Bool("false") #=> NoMethodError: undefined method `Bool' for Kernel
|
40
|
+
Bool(0) #=> NoMethodError: undefined method `Bool' for Kernel
|
41
|
+
|
28
42
|
"true".to_b #=> NoMethodError: undefined method `to_b' for String
|
29
43
|
1.to_b #=> NoMethodError: undefined method `to_b' for Integer
|
30
44
|
Bool("true") #=> NoMethodError: undefined method `Bool' for Kernel
|
31
45
|
Bool(1) #=> NoMethodError: undefined method `Bool' for Kernel
|
46
|
+
|
32
47
|
...
|
33
48
|
```
|
34
49
|
|
@@ -38,6 +53,13 @@ Why? Why not? Discuss.
|
|
38
53
|
|
39
54
|
## Usage
|
40
55
|
|
56
|
+
[String](#string) •
|
57
|
+
[Symbol](#symbol) •
|
58
|
+
[Integer](#integer) •
|
59
|
+
[Kernel](#kernel)
|
60
|
+
|
61
|
+
|
62
|
+
|
41
63
|
``` ruby
|
42
64
|
false.is_a?(Bool) #=> true
|
43
65
|
true.is_a?(Bool) #=> true
|
@@ -46,35 +68,168 @@ false.class.ancestors #=> [FalseClass, Bool, Object, Kernel, BasicObject]
|
|
46
68
|
|
47
69
|
# -or-
|
48
70
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
71
|
+
false.to_i #=> 0
|
72
|
+
true.to_i #=> 1
|
73
|
+
|
74
|
+
# -or-
|
53
75
|
|
54
76
|
"false".to_b #=> false
|
55
77
|
0.to_b #=> false
|
56
78
|
Bool("false") #=> false
|
57
79
|
Bool(0) #=> false
|
80
|
+
|
81
|
+
"true".to_b #=> true
|
82
|
+
1.to_b #=> true
|
83
|
+
Bool("true") #=> true
|
84
|
+
Bool(1) #=> true
|
58
85
|
```
|
59
86
|
|
87
|
+
|
60
88
|
How about handling errors on invalid bool values when converting / parsing?
|
61
89
|
|
62
|
-
1. `to_b` always
|
63
|
-
2. `parse_bool/to_bool` always returns `nil` if the conversion / parsing
|
64
|
-
3. `Bool()` always raises
|
90
|
+
1. `to_b` always returns a bool even if the conversion / parsing fails e.g. `true` (for numbers) and `false` (for strings) on error
|
91
|
+
2. `parse_bool / to_bool` always returns `nil` if the conversion / parsing fails
|
92
|
+
3. `Bool()` always raises an `ArgumentError` if the conversion / parsing fails
|
93
|
+
and a `TypeError` if the conversion is unsupported (e.g. expected required `parse_bool` method missing / undefined)
|
65
94
|
|
66
95
|
|
67
96
|
``` ruby
|
68
|
-
"2".to_b
|
69
|
-
"2".to_bool
|
70
|
-
2.
|
71
|
-
2.to_bool
|
72
|
-
Bool("2")
|
73
|
-
|
97
|
+
"2".to_b #=> false
|
98
|
+
"2".to_bool #=> nil
|
99
|
+
"2".to_bool.bool? #=> false
|
100
|
+
"2".to_bool.is_a?(Bool) #=> false
|
101
|
+
Bool("2") #=> ArgumentError: invalid value "2":String for Bool(); parse_bool failed (returns nil)
|
102
|
+
|
103
|
+
2.to_b #=> true
|
104
|
+
2.to_bool #=> nil
|
105
|
+
2.to_bool.bool? #=> false
|
106
|
+
2.to_bool.is_a?(Bool) #=> false
|
107
|
+
Bool(2) #=> ArgumentError: invalid value 2:Integer for Bool(); parse_bool failed (returns nil)
|
74
108
|
...
|
75
109
|
```
|
76
110
|
|
77
111
|
|
112
|
+
### String
|
113
|
+
|
114
|
+
- Returns `true` if string is one of these values: **t**, **true**, **on**, **y**, **yes**, **1**.
|
115
|
+
- Returns `false` if string is one of these values: **f**, **false**, **off**, **n**, **no**, **0**.
|
116
|
+
|
117
|
+
For invalid boolean string values `to_b` returns `false` by default.
|
118
|
+
See the "Handling Errors" section for more options.
|
119
|
+
|
120
|
+
Note: The `Bool.parse` method ignores leading and trailing spaces and upper and lower cases e.g. ` FaLSe ` is the same as `false`.
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
'1'.to_b #=> true
|
124
|
+
't'.to_b #=> true
|
125
|
+
'T'.to_b #=> true
|
126
|
+
'true'.to_b #=> true
|
127
|
+
'TRUE'.to_b #=> true
|
128
|
+
'on'.to_b #=> true
|
129
|
+
'ON'.to_b #=> true
|
130
|
+
'y'.to_b #=> true
|
131
|
+
'yes'.to_b #=> true
|
132
|
+
'YES'.to_b #=> true
|
133
|
+
|
134
|
+
' 1 '.to_b #=> true
|
135
|
+
' t '.to_b #=> true
|
136
|
+
' T '.to_b #=> true
|
137
|
+
' true '.to_b #=> true
|
138
|
+
' TRUE '.to_b #=> true
|
139
|
+
' on '.to_b #=> true
|
140
|
+
' ON '.to_b #=> true
|
141
|
+
' y '.to_b #=> true
|
142
|
+
'Y'.to_b #=> true
|
143
|
+
' Y '.to_b #=> true
|
144
|
+
' yes '.to_b #=> true
|
145
|
+
' YES '.to_b #=> true
|
146
|
+
|
147
|
+
'0'.to_b #=> false
|
148
|
+
'f'.to_b #=> false
|
149
|
+
'F'.to_b #=> false
|
150
|
+
'false'.to_b #=> false
|
151
|
+
'FALSE'.to_b #=> false
|
152
|
+
'off'.to_b #=> false
|
153
|
+
'OFF'.to_b #=> false
|
154
|
+
'n'.to_b #=> false
|
155
|
+
'N'.to_b #=> false
|
156
|
+
'no'.to_b #=> false
|
157
|
+
'NO'.to_b #=> false
|
158
|
+
|
159
|
+
' 0 '.to_b #=> false
|
160
|
+
' f '.to_b #=> false
|
161
|
+
' F '.to_b #=> false
|
162
|
+
' false '.to_b #=> false
|
163
|
+
' FALSE '.to_b #=> false
|
164
|
+
' off '.to_b #=> false
|
165
|
+
' OFF '.to_b #=> false
|
166
|
+
' n '.to_b #=> false
|
167
|
+
' N '.to_b #=> false
|
168
|
+
' no '.to_b #=> false
|
169
|
+
' NO '.to_b #=> false
|
170
|
+
|
171
|
+
''.to_b #=> false
|
172
|
+
' '.to_b #=> false
|
173
|
+
'xxx'.to_b #=> false
|
174
|
+
'bool'.to_b #=> false
|
175
|
+
|
176
|
+
''.to_bool #=> nil
|
177
|
+
' '.to_bool #=> nil
|
178
|
+
'xxx'.to_bool #=> nil
|
179
|
+
'bool'.to_bool #=> nil
|
180
|
+
```
|
181
|
+
|
182
|
+
### Symbol
|
183
|
+
|
184
|
+
Same as `self.to_s.to_b` or `self.to_s.to_bool`.
|
185
|
+
|
186
|
+
``` ruby
|
187
|
+
:'1'.to_b #=> true
|
188
|
+
:t.to_b #=> true
|
189
|
+
:true.to_b #=> true
|
190
|
+
:on.to_b #=> true
|
191
|
+
:y.to_b #=> true
|
192
|
+
:yes.to_b #=> true
|
193
|
+
|
194
|
+
:'0'.to_b #=> false
|
195
|
+
:f.to_b #=> false
|
196
|
+
:false.to_b #=> false
|
197
|
+
:off.to_b #=> false
|
198
|
+
:n.to_b #=> false
|
199
|
+
:no.to_b #=> false
|
200
|
+
|
201
|
+
:xxx.to_b #=> false
|
202
|
+
:bool.to_b #=> false
|
203
|
+
|
204
|
+
:xxx.to_bool #=> nil
|
205
|
+
:bool.to_bool #=> nil
|
206
|
+
```
|
207
|
+
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
### Integer
|
212
|
+
|
213
|
+
Returns `false` if the number is zero and `true` otherwise.
|
214
|
+
|
215
|
+
|
216
|
+
```ruby
|
217
|
+
0.to_b #=> false
|
218
|
+
1.to_b #=> true
|
219
|
+
2.to_b #=> true
|
220
|
+
-1.to_b #=> true
|
221
|
+
-2.to_b #=> true
|
222
|
+
|
223
|
+
0.to_bool #=> false
|
224
|
+
1.to_bool #=> true
|
225
|
+
|
226
|
+
2.to_bool #=> nil
|
227
|
+
-1.to_bool #=> nil
|
228
|
+
-2.to_bool #=> nil
|
229
|
+
```
|
230
|
+
|
231
|
+
|
232
|
+
### Kernel
|
78
233
|
|
79
234
|
More methods added to `Kernel` include `bool?`, `false?`, `true?`.
|
80
235
|
Example:
|
data/Rakefile
CHANGED
@@ -5,7 +5,7 @@ Hoe.spec 'safebool' do
|
|
5
5
|
|
6
6
|
self.version = SaferBool::VERSION
|
7
7
|
|
8
|
-
self.summary = "safebool - safe bool
|
8
|
+
self.summary = "safebool - safe bool / boolean type adds Bool(), to_b, parse_bool / to_bool, bool?, false?, true?, true.is_a?(Bool)==true, false.is_a?(Bool)==true, and more"
|
9
9
|
self.description = summary
|
10
10
|
|
11
11
|
self.urls = ['https://github.com/s6ruby/safebool']
|
data/lib/safebool.rb
CHANGED
@@ -30,27 +30,22 @@ module Bool
|
|
30
30
|
|
31
31
|
|
32
32
|
def self.convert( o ) ## used by "global" Bool( o ) kernel conversion method
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
nil
|
39
|
-
end
|
40
|
-
|
41
|
-
if value.nil?
|
42
|
-
raise TypeError.new( "cannot convert >#{o.inspect}< of type >#{o.class.name}< to Bool; method parse_bool/to_bool expected")
|
43
|
-
else
|
33
|
+
if o.respond_to?( :parse_bool )
|
34
|
+
value = o.parse_bool() # note: returns true/false OR nil
|
35
|
+
if value.nil?
|
36
|
+
raise ArgumentError.new( "invalid value >#{o.inspect}< of type >#{o.class.name}< for Bool(); method parse_bool failed (returns nil)")
|
37
|
+
end
|
44
38
|
value
|
39
|
+
else
|
40
|
+
raise TypeError.new( "can't convert >#{o.inspect}< of type >#{o.class.name}< to Bool; method parse_bool expected / missing")
|
45
41
|
end
|
46
42
|
end
|
47
43
|
|
48
|
-
def self.zero() false; end
|
44
|
+
def self.zero() false; end
|
49
45
|
|
50
46
|
|
51
47
|
def to_b() self; end
|
52
48
|
def parse_bool() self; end
|
53
|
-
alias_method :to_bool, :parse_bool
|
54
49
|
end # module Bool
|
55
50
|
|
56
51
|
|
@@ -59,12 +54,21 @@ class FalseClass
|
|
59
54
|
include Bool ## "hack" - enables false.is_a?(Bool)
|
60
55
|
|
61
56
|
def zero?() true; end
|
57
|
+
def to_i() 0; end
|
58
|
+
|
59
|
+
## note: include Bool does NOT include module function (e.g. self.zero, etc.)
|
60
|
+
## todo/check/discuss - also include self.parse - why? why not?
|
61
|
+
def self.zero() false; end ## lets you write: false.class.zero
|
62
62
|
end
|
63
63
|
|
64
64
|
class TrueClass
|
65
65
|
include Bool ## "hack" - enables true.is_a?(Bool)
|
66
66
|
|
67
67
|
def zero?() false; end
|
68
|
+
def to_i() 1; end
|
69
|
+
|
70
|
+
## note: include Bool does NOT include module function (e.g. self.zero, etc.)
|
71
|
+
def self.zero() false; end ## lets you write: true.class.zero
|
68
72
|
end
|
69
73
|
|
70
74
|
|
@@ -100,10 +104,29 @@ module Kernel
|
|
100
104
|
|
101
105
|
|
102
106
|
#####
|
103
|
-
#
|
104
|
-
def to_b
|
107
|
+
# default "explicit" conversion to bool for all objects
|
108
|
+
def to_b
|
109
|
+
if respond_to?( :parse_bool )
|
110
|
+
value = parse_bool() # note: returns true/false/nil (nil for error/cannot parse)
|
111
|
+
value = true if value.nil? # default nil (cannot parse to bool) to true
|
112
|
+
value
|
113
|
+
else
|
114
|
+
self ? true : false ## use "standard" ruby semantics, that is, everything is true except false & nil
|
115
|
+
end
|
116
|
+
end
|
105
117
|
|
106
118
|
|
119
|
+
# to_bool - "porcelain" method "alias" for parse_bool; use parse_bool for "internal" use and to_bool for "external" use
|
120
|
+
# note: by "default" the method parse_bool is undefined (!);
|
121
|
+
# define parse_bool in concrete / derived class to add bool conversion support
|
122
|
+
def to_bool()
|
123
|
+
if respond_to?( :parse_bool )
|
124
|
+
parse_bool()
|
125
|
+
else
|
126
|
+
nil ## note: returns nil if can't convert to true or false
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
107
130
|
### "global" conversion function / method
|
108
131
|
def Bool( o ) Bool.convert( o ); end
|
109
132
|
end
|
@@ -111,44 +134,41 @@ end
|
|
111
134
|
|
112
135
|
|
113
136
|
class NilClass
|
114
|
-
def to_b()
|
137
|
+
def to_b() false; end
|
115
138
|
def parse_bool() false; end
|
116
|
-
alias_method :to_bool, :parse_bool
|
117
139
|
end
|
118
140
|
|
119
141
|
class String
|
120
142
|
def to_b
|
121
143
|
value = parse_bool()
|
122
|
-
value
|
144
|
+
value = false if value.nil? ## note: return false (!) for all undefined / unknown string values that cannot convert to bool
|
145
|
+
value
|
123
146
|
end
|
124
147
|
def parse_bool() Bool.parse( self ); end
|
125
|
-
alias_method :to_bool, :parse_bool
|
126
148
|
end
|
127
149
|
|
128
150
|
class Symbol
|
129
|
-
def to_b()
|
151
|
+
def to_b() to_s.to_b; end
|
130
152
|
def parse_bool() to_s.parse_bool(); end
|
131
|
-
alias_method :to_bool, :parse_bool
|
132
153
|
end
|
133
154
|
|
134
|
-
class
|
135
|
-
def to_b
|
155
|
+
class Integer
|
156
|
+
def to_b
|
136
157
|
value = parse_bool()
|
137
|
-
value
|
158
|
+
value = true if value.nil? ## note return true for all undefined / unknown number/numeric values that cannot convert to bool
|
159
|
+
value
|
138
160
|
end
|
139
|
-
def parse_bool
|
161
|
+
def parse_bool
|
140
162
|
if self == 0
|
141
163
|
false
|
142
164
|
elsif self == 1
|
143
165
|
true
|
144
166
|
else
|
145
|
-
nil ## note: returns nil if
|
167
|
+
nil ## note: returns nil if can't convert to true or false
|
146
168
|
end
|
147
169
|
end
|
148
|
-
alias_method :to_bool, :parse_bool
|
149
170
|
end
|
150
171
|
|
151
172
|
|
152
173
|
|
153
|
-
|
154
174
|
puts SaferBool.banner ## say hello
|
data/lib/safebool/version.rb
CHANGED
data/test/test_bool.rb
CHANGED
@@ -11,33 +11,63 @@ require 'helper'
|
|
11
11
|
class TestBool < MiniTest::Test
|
12
12
|
|
13
13
|
def test_bool
|
14
|
-
assert_equal true,
|
15
|
-
assert_equal true,
|
16
|
-
assert_equal false,
|
17
|
-
assert_equal true,
|
18
|
-
assert_equal false,
|
19
|
-
|
20
|
-
assert_equal
|
21
|
-
|
22
|
-
assert_equal
|
23
|
-
assert_equal true,
|
24
|
-
assert_equal
|
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
|
+
assert_equal 0, false.to_i
|
20
|
+
assert_equal "false", false.to_s
|
21
|
+
|
22
|
+
assert_equal true, true.is_a?(Bool)
|
23
|
+
assert_equal true, true.true?
|
24
|
+
assert_equal false, true.false?
|
25
|
+
assert_equal true, true.bool?
|
26
|
+
assert_equal true, true.to_b
|
27
|
+
assert_equal 1, true.to_i
|
28
|
+
assert_equal "true", true.to_s
|
29
|
+
|
30
|
+
assert_equal false, Bool.zero
|
31
|
+
assert_equal false, false.class.zero
|
32
|
+
assert_equal false, true.class.zero
|
33
|
+
|
34
|
+
assert_equal true, false.zero?
|
35
|
+
assert_equal false, true.zero?
|
36
|
+
|
37
|
+
assert_equal true, false.frozen? ## (always) true by default
|
38
|
+
assert_equal true, true.frozen? ## (always) true by default
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_parse
|
42
|
+
assert_equal false, Bool.parse( "false" )
|
43
|
+
assert_equal false, Bool.parse( "f" )
|
44
|
+
assert_equal false, Bool.parse( "0" )
|
45
|
+
|
46
|
+
assert_equal true, Bool.parse( "true" )
|
47
|
+
assert_equal true, Bool.parse( "t" )
|
48
|
+
assert_equal true, Bool.parse( "1" )
|
49
|
+
|
50
|
+
assert_equal true, Bool.parse( "" ) == nil
|
51
|
+
assert_equal true, Bool.parse( "2" ) == nil
|
52
|
+
assert_equal true, Bool.parse( "xxx" ) == nil
|
25
53
|
end
|
26
54
|
|
27
55
|
def test_nil
|
28
56
|
assert_equal false, nil.to_b
|
57
|
+
assert_equal false, nil.to_bool
|
29
58
|
assert_equal false, Bool(nil)
|
30
59
|
|
60
|
+
assert_equal 0, nil.to_i
|
61
|
+
assert_equal "", nil.to_s
|
62
|
+
|
63
|
+
assert_equal true, nil.to_bool.is_a?(Bool)
|
64
|
+
|
31
65
|
assert_equal false, nil.false?
|
32
66
|
assert_equal false, nil.true?
|
33
67
|
assert_equal false, nil.bool?
|
34
68
|
end
|
35
69
|
|
36
70
|
def test_string
|
37
|
-
assert_equal false, "true".false?
|
38
|
-
assert_equal false, "true".true?
|
39
|
-
assert_equal false, "true".bool?
|
40
|
-
|
41
71
|
assert_equal true, "true".to_b
|
42
72
|
assert_equal true, "yes".to_b
|
43
73
|
assert_equal true, "on".to_b
|
@@ -57,13 +87,28 @@ class TestBool < MiniTest::Test
|
|
57
87
|
assert_equal true, Bool("t")
|
58
88
|
assert_equal true, Bool("y")
|
59
89
|
assert_equal true, Bool("1")
|
90
|
+
|
91
|
+
assert_equal true, "".to_bool == nil
|
92
|
+
assert_equal true, " ".to_bool == nil
|
93
|
+
assert_equal true, "2".to_bool == nil
|
94
|
+
assert_equal true, "xxx".to_bool == nil
|
95
|
+
|
96
|
+
assert_equal false, "".to_bool.bool?
|
97
|
+
assert_equal false, " ".to_bool.bool?
|
98
|
+
assert_equal false, "2".to_bool.bool?
|
99
|
+
assert_equal false, "xxx".to_bool.bool?
|
100
|
+
|
101
|
+
assert_equal false, "".to_bool.is_a?(Bool)
|
102
|
+
assert_equal false, " ".to_bool.is_a?(Bool)
|
103
|
+
assert_equal false, "2".to_bool.is_a?(Bool)
|
104
|
+
assert_equal false, "xxx".to_bool.is_a?(Bool)
|
105
|
+
|
106
|
+
assert_equal false, "true".false?
|
107
|
+
assert_equal false, "true".true?
|
108
|
+
assert_equal false, "true".bool?
|
60
109
|
end
|
61
110
|
|
62
111
|
def test_symbol
|
63
|
-
assert_equal false, :true.false?
|
64
|
-
assert_equal false, :true.true?
|
65
|
-
assert_equal false, :true.bool?
|
66
|
-
|
67
112
|
assert_equal true, :true.to_b
|
68
113
|
assert_equal true, :yes.to_b
|
69
114
|
assert_equal true, :on.to_b
|
@@ -77,20 +122,45 @@ class TestBool < MiniTest::Test
|
|
77
122
|
assert_equal true, Bool(:t)
|
78
123
|
assert_equal true, Bool(:y)
|
79
124
|
assert_equal true, Bool(:"1")
|
125
|
+
|
126
|
+
assert_equal true, :'2'.to_bool == nil
|
127
|
+
assert_equal true, :xxx.to_bool == nil
|
128
|
+
|
129
|
+
assert_equal false, :'2'.to_bool.bool?
|
130
|
+
assert_equal false, :'xxx'.to_bool.bool?
|
131
|
+
|
132
|
+
assert_equal false, :'2'.to_bool.is_a?(Bool)
|
133
|
+
assert_equal false, :'xxx'.to_bool.is_a?(Bool)
|
134
|
+
|
135
|
+
assert_equal false, :true.false?
|
136
|
+
assert_equal false, :true.true?
|
137
|
+
assert_equal false, :true.bool?
|
80
138
|
end
|
81
139
|
|
82
140
|
|
83
141
|
def test_integer
|
84
|
-
assert_equal false, 1.false?
|
85
|
-
assert_equal false, 1.true?
|
86
|
-
assert_equal false, 1.bool?
|
87
|
-
|
88
142
|
assert_equal false, 0.to_b
|
89
143
|
assert_equal true, 1.to_b
|
90
144
|
assert_equal true, 2.to_b
|
91
145
|
|
92
146
|
assert_equal false, Bool(0)
|
93
147
|
assert_equal true, Bool(1)
|
94
|
-
|
148
|
+
|
149
|
+
assert_equal true, 2.to_bool == nil
|
150
|
+
assert_equal true, -1.to_bool == nil
|
151
|
+
|
152
|
+
assert_equal false, 2.to_bool.bool?
|
153
|
+
assert_equal false, -1.to_bool.bool?
|
154
|
+
|
155
|
+
assert_equal false, 2.to_bool.is_a?(Bool)
|
156
|
+
assert_equal false, -1.to_bool.is_a?(Bool)
|
157
|
+
|
158
|
+
assert_equal false, 1.false?
|
159
|
+
assert_equal false, 1.true?
|
160
|
+
assert_equal false, 1.bool?
|
161
|
+
|
162
|
+
assert_equal false, 0.false?
|
163
|
+
assert_equal false, 0.true?
|
164
|
+
assert_equal false, 0.bool?
|
95
165
|
end
|
96
166
|
end # class TestBool
|
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.1.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-
|
11
|
+
date: 2019-04-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rdoc
|
@@ -38,8 +38,8 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '3.16'
|
41
|
-
description: safebool - safe bool
|
42
|
-
true?, true.is_a?(Bool)==true, false.is_a?(Bool)==true, and more
|
41
|
+
description: safebool - safe bool / boolean type adds Bool(), to_b, parse_bool / to_bool,
|
42
|
+
bool?, false?, true?, true.is_a?(Bool)==true, false.is_a?(Bool)==true, and more
|
43
43
|
email: wwwmake@googlegroups.com
|
44
44
|
executables: []
|
45
45
|
extensions: []
|
@@ -83,6 +83,6 @@ rubyforge_project:
|
|
83
83
|
rubygems_version: 2.5.2
|
84
84
|
signing_key:
|
85
85
|
specification_version: 4
|
86
|
-
summary: safebool - safe bool
|
87
|
-
true?, true.is_a?(Bool)==true, false.is_a?(Bool)==true, and more
|
86
|
+
summary: safebool - safe bool / boolean type adds Bool(), to_b, parse_bool / to_bool,
|
87
|
+
bool?, false?, true?, true.is_a?(Bool)==true, false.is_a?(Bool)==true, and more
|
88
88
|
test_files: []
|