chainedexpressions 0.2 → 0.3
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.
- checksums.yaml +4 -4
- data/lib/chainedexpressions/chainedexpressions.rb +31 -22
- data/lib/chainedexpressions/version.rb +1 -1
- data/spec/chainedexpressions_spec.rb +12 -23
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a0080d860307a431d8e18cb9960c58e891b2e8ec
|
4
|
+
data.tar.gz: c14be71435652bf0f82c42dc9b1b5fe70461e37e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9744d7c4d860e29fc650c1baa30aecc2cbd96ce19983e57d0c12c8843f0ad3df313a6fec06c89d876fb77465d767e556cb81e82072909757632ac2590d0cba99
|
7
|
+
data.tar.gz: c4b5aa883017e5d0d9becfbed909997af89fb29a2148332a0b0925b71060e364edbffbb66497e5bdc1f00670cbf66dddf1f322af598c9489dc32cb4c8f23715a
|
@@ -13,40 +13,49 @@ module ChainedExpressions
|
|
13
13
|
:< => :lt,
|
14
14
|
:<= => :lte,
|
15
15
|
:>= => :gte,
|
16
|
-
:> => :gt
|
16
|
+
:> => :gt
|
17
17
|
}
|
18
18
|
|
19
|
+
refine Numeric do
|
20
|
+
def eval_expression(op, n)
|
21
|
+
case op
|
22
|
+
when :< then (self <=> n) == -1
|
23
|
+
when :<= then (self <=> n) == -1 || (self <=> n) == 0
|
24
|
+
when :>= then (self <=> n) == 0 || (self <=> n) == 1
|
25
|
+
when :> then (self <=> n) == 1
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
19
30
|
%w(TrueClass FalseClass).each { |c|
|
20
31
|
eval <<-EOM
|
21
32
|
refine #{c} do
|
22
|
-
$operators.each { |
|
23
|
-
define_method o do |n|
|
24
|
-
|
33
|
+
$operators.each { |op, o|
|
34
|
+
define_method o do |n|
|
35
|
+
prev = $prev
|
36
|
+
$prev = n
|
37
|
+
prev.eval_expression op, n
|
25
38
|
end
|
26
39
|
|
27
|
-
alias_method
|
40
|
+
alias_method op, o
|
28
41
|
}
|
29
42
|
end
|
30
43
|
EOM
|
31
44
|
}
|
32
45
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
end
|
46
|
+
%w(Fixnum Float Bignum).each { |c|
|
47
|
+
eval <<-EOM
|
48
|
+
refine #{c} do
|
49
|
+
$operators.each { |op, o|
|
50
|
+
define_method o do |n|
|
51
|
+
$prev = n
|
52
|
+
eval_expression op, n
|
53
|
+
end
|
42
54
|
|
43
|
-
|
44
|
-
|
45
|
-
$f = n
|
46
|
-
self.exp_true? k, n
|
55
|
+
alias_method op, o
|
56
|
+
}
|
47
57
|
end
|
58
|
+
EOM
|
59
|
+
}
|
60
|
+
end
|
48
61
|
|
49
|
-
alias_method k, o
|
50
|
-
}
|
51
|
-
end
|
52
|
-
end
|
@@ -10,39 +10,28 @@
|
|
10
10
|
|
11
11
|
require 'rspec'
|
12
12
|
require 'chainedexpressions'
|
13
|
-
|
14
13
|
using ChainedExpressions
|
15
14
|
|
16
15
|
describe ChainedExpressions do
|
17
16
|
it 'can evaluate true expressions' do
|
18
|
-
(20 < 25).should be true
|
19
|
-
|
20
|
-
(20 < 25 < 30).should be true
|
21
|
-
(20 < 25 < 30).should be true
|
22
|
-
|
23
|
-
(20 <= 25 <= 30 <= 30 <= 35).should be true
|
24
|
-
(20 <= 25 <= 30 <= 30 <= 35).should be true
|
25
|
-
|
26
|
-
(35 > 30 > 25 > 20).should be true
|
17
|
+
(20 < 25 ).should be true
|
18
|
+
(20 < 25 < 30 ).should be true
|
27
19
|
(35 > 30 > 25 > 20).should be true
|
28
20
|
|
29
|
-
(
|
30
|
-
(35
|
21
|
+
(20 <= 25 <= 30 <= 30 <= 35).should be true
|
22
|
+
(35 >= 30 >= 30 >= 25 >= 20).should be true
|
23
|
+
(20.5 >= 10 >= 2.5 >= 2.5 ).should be true
|
31
24
|
end
|
32
25
|
|
33
26
|
it 'can evaluate false expressions' do
|
34
|
-
(30 < 25).should be false
|
35
|
-
|
36
|
-
(30 < 25 < 20).should be false
|
37
|
-
(30 < 25 < 20).should be false
|
38
|
-
|
39
|
-
(35 <= 30 <= 30 <= 25 <= 20).should be false
|
40
|
-
(35 <= 30 <= 30 <= 25 <= 20).should be false
|
41
|
-
|
42
|
-
(20 > 25 > 30 > 35).should be false
|
27
|
+
(30 < 25 ).should be false
|
28
|
+
(30 < 25 < 20 ).should be false
|
43
29
|
(20 > 25 > 30 > 35).should be false
|
44
30
|
|
45
|
-
(
|
46
|
-
(20
|
31
|
+
(35 <= 30 <= 30 <= 25 <= 20).should be false
|
32
|
+
(20 >= 25 >= 30 >= 30 >= 35).should be false
|
33
|
+
(20.5 >= 10 >= 2.5 >= 3.5 ).should be false
|
34
|
+
|
35
|
+
([1, 2])
|
47
36
|
end
|
48
37
|
end
|