custom_boolean 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +0 -1
- data/README.markdown +28 -21
- data/lib/custom_boolean.rb +24 -14
- data/lib/custom_boolean/version.rb +1 -1
- data/test/test.rb +3 -0
- metadata +3 -6
data/CHANGELOG
CHANGED
data/README.markdown
CHANGED
@@ -1,16 +1,22 @@
|
|
1
1
|
Custom Boolean
|
2
2
|
==============
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
Tired of the way Ruby sees truth? Want to experiment with the
|
5
|
+
way other languages see it? No? Oh well, you may still want to have a play...
|
6
|
+
|
7
|
+
CustomBoolean is a little hack to bring user-defined truthiness to
|
8
|
+
`if/else_if/else`-like expressions.
|
6
9
|
|
7
10
|
* Implements various preset truth tests: Ruby, Python, Perl, C, Strict Boolean.
|
8
11
|
* Provides ability to completely customize truthiness.
|
9
12
|
* Provides common Boolean Operators (and, or, not) compatible with CustomBoolean
|
13
|
+
* Read the [documentation](http://rdoc.info/github/banister/custom_boolean/master/frames)
|
14
|
+
* See the [source code](http://github.com/banister/custom_boolean)
|
15
|
+
* Install the [gem](https://rubygems.org/gems/custom_boolean) `gem install custom_boolean`
|
10
16
|
|
11
17
|
Normal conditionals:
|
12
18
|
--------------------
|
13
|
-
|
19
|
+
if_(0) {
|
14
20
|
puts 'true'
|
15
21
|
}.
|
16
22
|
else {
|
@@ -25,7 +31,7 @@ A Pythonic truthiness:
|
|
25
31
|
# redefine truthiness with the `truth_test` method
|
26
32
|
CustomBoolean.truth_test = CustomBoolean::PYTHON_TRUTH
|
27
33
|
|
28
|
-
|
34
|
+
if_(0) {
|
29
35
|
puts 'true'
|
30
36
|
}.
|
31
37
|
else {
|
@@ -38,7 +44,7 @@ A full example:
|
|
38
44
|
------------------------
|
39
45
|
|
40
46
|
x = 5
|
41
|
-
|
47
|
+
if_(x == 4) {
|
42
48
|
puts 'x is 4'
|
43
49
|
}.
|
44
50
|
else_if(x == 5) {
|
@@ -57,11 +63,11 @@ Nested ifs work fine:
|
|
57
63
|
x = :delighted
|
58
64
|
y = :aroused
|
59
65
|
|
60
|
-
|
66
|
+
if_(x == :sad) {
|
61
67
|
puts 'evaluates to true'
|
62
68
|
}.
|
63
69
|
else_if(x == :delighted) {
|
64
|
-
|
70
|
+
if_(y == :happy) {
|
65
71
|
puts 'delighted and happy'
|
66
72
|
}.
|
67
73
|
else_if(y == :aroused) {
|
@@ -72,13 +78,13 @@ Nested ifs work fine:
|
|
72
78
|
else {
|
73
79
|
puts 'not delighted'
|
74
80
|
}
|
75
|
-
|
81
|
+
#=> "delighted and aroused"
|
76
82
|
|
77
83
|
If expressions
|
78
84
|
----------------
|
79
85
|
|
80
|
-
# prefixing `
|
81
|
-
+
|
86
|
+
# prefixing `if_` with `+` invokes if-expression behaviour
|
87
|
+
+if_(false) {
|
82
88
|
:hello
|
83
89
|
}.
|
84
90
|
else {
|
@@ -99,7 +105,7 @@ Use as follows:
|
|
99
105
|
|
100
106
|
CustomBoolean.truth_test = CustomBoolean::C_TRUTH
|
101
107
|
|
102
|
-
+
|
108
|
+
+if_(0) {
|
103
109
|
true
|
104
110
|
}.
|
105
111
|
else {
|
@@ -116,7 +122,7 @@ Customizable truthiness
|
|
116
122
|
# only :horse is true
|
117
123
|
CustomBoolean.truth_test = proc { |b| b == :horse }
|
118
124
|
|
119
|
-
|
125
|
+
if_(true) {
|
120
126
|
puts 'evaluates to true'
|
121
127
|
}.
|
122
128
|
else {
|
@@ -124,7 +130,7 @@ Customizable truthiness
|
|
124
130
|
}
|
125
131
|
# => 'reached else
|
126
132
|
|
127
|
-
|
133
|
+
if_(:horse) {
|
128
134
|
puts 'evaluates to true'
|
129
135
|
}.
|
130
136
|
else {
|
@@ -132,17 +138,18 @@ Customizable truthiness
|
|
132
138
|
}
|
133
139
|
#=> 'evaluates to true'
|
134
140
|
|
141
|
+
|
135
142
|
Boolean operators
|
136
143
|
-----------------
|
137
144
|
|
138
|
-
The ordinary
|
139
|
-
CustomBoolean truthiness. Instead use
|
140
|
-
|
145
|
+
The ordinary && and || and ! operators do not implement
|
146
|
+
CustomBoolean truthiness. Instead use Object#and, Object#or, and
|
147
|
+
Object#negate (or Object#& and Object#| and true.& and false.|, etc)
|
141
148
|
|
142
149
|
# use perl truthiness where "0" is false
|
143
150
|
CustomBoolean.truth_test = CustomBoolean::PERL_TRUTH
|
144
151
|
|
145
|
-
+
|
152
|
+
+if_(true & "0") {
|
146
153
|
true
|
147
154
|
}.
|
148
155
|
else {
|
@@ -151,23 +158,23 @@ CustomBoolean truthiness. Instead use `Object#and`, `Object#or`, and
|
|
151
158
|
#=> false
|
152
159
|
|
153
160
|
# Or use Object#and
|
154
|
-
+
|
161
|
+
+if_(true.and "0") {...}
|
155
162
|
|
156
163
|
# Use negate (rather than not)
|
157
|
-
+
|
164
|
+
+if_(negate("0")) { true } ##=> true
|
158
165
|
|
159
166
|
Testing truthiness of expressions
|
160
167
|
----------------------------------
|
161
168
|
|
162
169
|
You can test the truthiness of an expression without using the
|
163
|
-
full machinery of
|
170
|
+
full machinery of if_/else_if/else. Just use the
|
164
171
|
`CustomBoolean.truthy?()` function:
|
165
172
|
|
166
173
|
CustomBoolean.truth_test = CustomBoolean::PERL_TRUTH
|
167
174
|
CustomBoolean.truthy?("0") #=> false
|
168
175
|
|
169
176
|
Feedback
|
170
|
-
|
177
|
+
---------
|
171
178
|
|
172
179
|
Problems or bugs, file an issue!
|
173
180
|
|
data/lib/custom_boolean.rb
CHANGED
@@ -97,13 +97,7 @@ class CustomBoolean
|
|
97
97
|
|
98
98
|
class << self
|
99
99
|
|
100
|
-
#
|
101
|
-
attr_accessor :truth_test
|
102
|
-
|
103
|
-
# Tests whether *condition* is truthy according to
|
104
|
-
# CustomBoolean truthiness.
|
105
|
-
# CustomBoolean truthiness is determined by the proc referenced
|
106
|
-
# by CustomBoolean.truth_test.
|
100
|
+
# Determines the truth test to apply (for CustomBoolean truthiness)
|
107
101
|
#
|
108
102
|
# Built in Truth tests include:
|
109
103
|
#
|
@@ -119,13 +113,29 @@ class CustomBoolean
|
|
119
113
|
#
|
120
114
|
# @example changing truthiness to a preset
|
121
115
|
# CustomBoolean.truth_test = CustomBoolean::PYTHON_TRUTH
|
122
|
-
# @example user-defined truthiness
|
116
|
+
# @example user-defined truthiness
|
123
117
|
# # only :horse is true:
|
124
118
|
# CustomBoolean.truth_test = proc { |expr| expr == :horse }
|
125
|
-
# @param
|
119
|
+
# @param [Proc] truth_test_proc The Proc that defines the truth test
|
120
|
+
# @return [Proc] The proc that defines the truth test
|
121
|
+
attr_accessor :truth_test
|
122
|
+
|
123
|
+
# Tests whether *expression* is truthy according to
|
124
|
+
# CustomBoolean truthiness.
|
125
|
+
# CustomBoolean truthiness is determined by the proc referenced
|
126
|
+
# by CustomBoolean.truth_test.
|
127
|
+
# @param expr an expression to evaluate
|
126
128
|
# @return [Boolean]
|
127
|
-
|
128
|
-
|
129
|
+
# @example using C truthiness
|
130
|
+
# CustomBoolean.truth_test = CustomBoolean::C_TRUTH
|
131
|
+
# CustomBoolean.truthy?(0) #=> false
|
132
|
+
# @example defining and using *horse* truthiness :)
|
133
|
+
# # only :horse is *true*
|
134
|
+
# CustomBoolean.truth_test = proc { |expr| expr == :horse }
|
135
|
+
# CustomBoolean.truthy?(:horse) #=> true
|
136
|
+
# CustomBoolean.truthy?(true) #=> false
|
137
|
+
def truthy?(expr)
|
138
|
+
self.truth_test.call(expr)
|
129
139
|
end
|
130
140
|
end
|
131
141
|
|
@@ -152,10 +162,10 @@ class CustomBoolean
|
|
152
162
|
# @return [Object] extracts the value of the if, transforming it
|
153
163
|
# into an if-expression
|
154
164
|
# @example single if-expression example
|
155
|
-
# +
|
156
|
-
# +
|
165
|
+
# +if_(true) { :hello } #=> :hello
|
166
|
+
# +if_(false) { :hello } #=> nil
|
157
167
|
# @example if-else-expression example
|
158
|
-
# +
|
168
|
+
# +if_(false) {
|
159
169
|
# :hello
|
160
170
|
# }.
|
161
171
|
# else {
|
data/test/test.rb
CHANGED
@@ -295,9 +295,12 @@ class CustomBooleanTest < Test::Unit::TestCase
|
|
295
295
|
}
|
296
296
|
assert_equal(true, val.value)
|
297
297
|
end
|
298
|
+
CustomBoolean.truth_test = CustomBoolean::RUBY_TRUTH
|
298
299
|
end
|
299
300
|
|
300
301
|
def test_ruby_truth
|
302
|
+
CustomBoolean.truth_test = CustomBoolean::RUBY_TRUTH
|
303
|
+
|
301
304
|
[nil, false].each do |v|
|
302
305
|
val = if?(v) {
|
303
306
|
true
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: custom_boolean
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 19
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
7
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
8
|
+
- 5
|
9
|
+
version: 0.1.5
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- John Mair (banisterfiend)
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2010-09-
|
17
|
+
date: 2010-09-29 00:00:00 +13:00
|
19
18
|
default_executable:
|
20
19
|
dependencies: []
|
21
20
|
|
@@ -53,7 +52,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
53
52
|
requirements:
|
54
53
|
- - ">="
|
55
54
|
- !ruby/object:Gem::Version
|
56
|
-
hash: 3
|
57
55
|
segments:
|
58
56
|
- 0
|
59
57
|
version: "0"
|
@@ -62,7 +60,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
60
|
requirements:
|
63
61
|
- - ">="
|
64
62
|
- !ruby/object:Gem::Version
|
65
|
-
hash: 3
|
66
63
|
segments:
|
67
64
|
- 0
|
68
65
|
version: "0"
|