custom_boolean 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/examples/example_horse.rb +18 -0
- data/examples/example_nested.rb +21 -0
- data/examples/example_operators.rb +11 -0
- data/lib/custom_boolean.rb +22 -16
- data/lib/custom_boolean/version.rb +1 -1
- data/test/test.rb +98 -0
- metadata +6 -6
@@ -0,0 +1,18 @@
|
|
1
|
+
require '../lib/custom_boolean'
|
2
|
+
|
3
|
+
# only :horse is true
|
4
|
+
CustomBoolean.truth_test = proc { |b| b == :horse }
|
5
|
+
|
6
|
+
if?(true) {
|
7
|
+
puts 'evaluates to true'
|
8
|
+
}.
|
9
|
+
else {
|
10
|
+
puts 'reached else'
|
11
|
+
}
|
12
|
+
|
13
|
+
if?(:horse) {
|
14
|
+
puts 'evaluates to true'
|
15
|
+
}.
|
16
|
+
else {
|
17
|
+
puts 'reached else'
|
18
|
+
}
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require '../lib/custom_boolean'
|
2
|
+
|
3
|
+
x = :delighted
|
4
|
+
y = :aroused
|
5
|
+
|
6
|
+
if?(x == :sad) {
|
7
|
+
puts 'evaluates to true'
|
8
|
+
}.
|
9
|
+
else_if(x == :delighted) {
|
10
|
+
if?(y == :happy) {
|
11
|
+
puts 'delighted and happy'
|
12
|
+
}.
|
13
|
+
else_if(y == :aroused) {
|
14
|
+
puts 'delighted and aroused'
|
15
|
+
}.
|
16
|
+
else { puts 'just delighted' }
|
17
|
+
}.
|
18
|
+
else {
|
19
|
+
puts 'not delighted'
|
20
|
+
}
|
21
|
+
|
data/lib/custom_boolean.rb
CHANGED
@@ -1,26 +1,32 @@
|
|
1
1
|
# CustomBoolean by John Mair (banisterfiend)
|
2
2
|
# MIT license
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
4
|
+
|
5
|
+
direc = File.dirname(__FILE__)
|
6
|
+
require "#{direc}/custom_boolean/version"
|
7
|
+
|
8
|
+
class CustomBoolean
|
9
|
+
module Operators
|
10
|
+
def &(other)
|
11
|
+
CustomBoolean.truthy?(self) && CustomBoolean.truthy?(other)
|
12
|
+
end
|
13
|
+
alias_method :and, :"&"
|
14
|
+
|
15
|
+
def |(other)
|
16
|
+
CustomBoolean.truthy?(self) || CustomBoolean.truthy?(other)
|
17
|
+
end
|
18
|
+
alias_method :or, :"|"
|
12
19
|
end
|
13
|
-
alias or |
|
14
20
|
end
|
15
21
|
|
16
|
-
true.extend(
|
17
|
-
false.extend(
|
18
|
-
Object.send(:include,
|
22
|
+
true.extend(CustomBoolean::Operators)
|
23
|
+
false.extend(CustomBoolean::Operators)
|
24
|
+
Object.send(:include, CustomBoolean::Operators)
|
19
25
|
|
20
|
-
def negate(
|
21
|
-
!CustomBoolean.truthy?(
|
26
|
+
def negate(expr)
|
27
|
+
!CustomBoolean.truthy?(expr)
|
22
28
|
end
|
23
|
-
|
29
|
+
|
24
30
|
def if_(condition, &block)
|
25
31
|
truth = !!CustomBoolean.truthy?(condition)
|
26
32
|
bvalue = block.call if truth
|
@@ -52,7 +58,7 @@ class CustomBoolean
|
|
52
58
|
STRICT_TRUTH = proc { |expr| raise "Expression must be strictly true or false." if expr != true && expr != false; expr }
|
53
59
|
PERL_TRUTH = proc { |expr| expr && expr != 0 && expr != "" && expr != "0" }
|
54
60
|
C_TRUTH = proc { |expr| expr && expr != 0 }
|
55
|
-
|
61
|
+
|
56
62
|
# default truth test is Ruby's
|
57
63
|
self.truth_test = RUBY_TRUTH
|
58
64
|
|
data/test/test.rb
CHANGED
@@ -319,6 +319,104 @@ class CustomBooleanTest < Test::Unit::TestCase
|
|
319
319
|
end
|
320
320
|
CustomBoolean.truth_test = CustomBoolean::RUBY_TRUTH
|
321
321
|
end
|
322
|
+
|
323
|
+
def test_perl_truth_operators
|
324
|
+
CustomBoolean.truth_test = CustomBoolean::PERL_TRUTH
|
325
|
+
|
326
|
+
truthy_value = "0"
|
327
|
+
|
328
|
+
[:and, :or].each do |o|
|
329
|
+
v = +if?(truthy_value.send(o, true)) {
|
330
|
+
true
|
331
|
+
}.
|
332
|
+
else! {
|
333
|
+
false
|
334
|
+
}
|
335
|
+
|
336
|
+
assert_equal(true, v) if o == :or
|
337
|
+
assert_equal(false, v) if o == :and
|
338
|
+
end
|
339
|
+
|
340
|
+
v = +if?(negate(truthy_value)) {
|
341
|
+
true
|
342
|
+
}.
|
343
|
+
else! {
|
344
|
+
false
|
345
|
+
}
|
346
|
+
|
347
|
+
assert_equal(true, v)
|
348
|
+
|
349
|
+
CustomBoolean.truth_test = CustomBoolean::RUBY_TRUTH
|
350
|
+
end
|
351
|
+
|
352
|
+
def test_python_truth_operators
|
353
|
+
CustomBoolean.truth_test = CustomBoolean::PYTHON_TRUTH
|
354
|
+
|
355
|
+
truthy_value = []
|
356
|
+
[:and, :or].each do |o|
|
357
|
+
v = +if?(truthy_value.send(o, true)) {
|
358
|
+
true
|
359
|
+
}.
|
360
|
+
else! {
|
361
|
+
false
|
362
|
+
}
|
363
|
+
|
364
|
+
assert_equal(true, v) if o == :or
|
365
|
+
assert_equal(false, v) if o == :and
|
366
|
+
end
|
367
|
+
|
368
|
+
v = +if?(negate(truthy_value)) {
|
369
|
+
true
|
370
|
+
}.
|
371
|
+
else! {
|
372
|
+
false
|
373
|
+
}
|
374
|
+
|
375
|
+
assert_equal(true, v)
|
376
|
+
|
377
|
+
CustomBoolean.truth_test = CustomBoolean::RUBY_TRUTH
|
378
|
+
end
|
379
|
+
|
380
|
+
def test_c_truth_operators
|
381
|
+
CustomBoolean.truth_test = CustomBoolean::C_TRUTH
|
382
|
+
|
383
|
+
truthy_value = 0
|
384
|
+
[:and, :or].each do |o|
|
385
|
+
v = +if?(truthy_value.send(o, true)) {
|
386
|
+
true
|
387
|
+
}.
|
388
|
+
else! {
|
389
|
+
false
|
390
|
+
}
|
391
|
+
|
392
|
+
assert_equal(true, v) if o == :or
|
393
|
+
assert_equal(false, v) if o == :and
|
394
|
+
end
|
395
|
+
|
396
|
+
v = +if?(negate(truthy_value)) {
|
397
|
+
true
|
398
|
+
}.
|
399
|
+
else! {
|
400
|
+
false
|
401
|
+
}
|
402
|
+
|
403
|
+
assert_equal(true, v)
|
404
|
+
|
405
|
+
CustomBoolean.truth_test = CustomBoolean::RUBY_TRUTH
|
406
|
+
end
|
407
|
+
|
408
|
+
def test_if_expression
|
409
|
+
CustomBoolean.truth_test = CustomBoolean::RUBY_TRUTH
|
410
|
+
|
411
|
+
v = +if?(true) {
|
412
|
+
:hero
|
413
|
+
}.
|
414
|
+
else {
|
415
|
+
:ostrich
|
416
|
+
}
|
417
|
+
|
418
|
+
assert_equal(:hero, v)
|
419
|
+
end
|
322
420
|
end
|
323
421
|
|
324
422
|
|
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: 27
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
7
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
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-28 00:00:00 +13:00
|
19
18
|
default_executable:
|
20
19
|
dependencies: []
|
21
20
|
|
@@ -34,6 +33,9 @@ files:
|
|
34
33
|
- lib/custom_boolean.rb
|
35
34
|
- lib/custom_boolean/version.rb
|
36
35
|
- examples/example.rb
|
36
|
+
- examples/example_horse.rb
|
37
|
+
- examples/example_nested.rb
|
38
|
+
- examples/example_operators.rb
|
37
39
|
- test/test.rb
|
38
40
|
has_rdoc: true
|
39
41
|
homepage: http://banisterfiend.wordpress.com
|
@@ -49,7 +51,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
49
51
|
requirements:
|
50
52
|
- - ">="
|
51
53
|
- !ruby/object:Gem::Version
|
52
|
-
hash: 3
|
53
54
|
segments:
|
54
55
|
- 0
|
55
56
|
version: "0"
|
@@ -58,7 +59,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
59
|
requirements:
|
59
60
|
- - ">="
|
60
61
|
- !ruby/object:Gem::Version
|
61
|
-
hash: 3
|
62
62
|
segments:
|
63
63
|
- 0
|
64
64
|
version: "0"
|