arithmetic 0.1.1 → 0.1.2
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 +8 -8
- data/.gitignore +1 -0
- data/Gemfile.lock +1 -1
- data/lib/arithmetic.rb +2 -2
- data/lib/arithmetic/parser.rb +6 -5
- data/lib/arithmetic/version.rb +1 -1
- data/spec/arithmetic_spec.rb +14 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NzVjMzE4ZDlmZGEzNGY5MDQ4Y2M4NGNmODk3NWI3OWQ5NDk5MGY1MA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
OGYxMjYyNGZmOTc4ZThhN2U0ZmZkMDk3ODAwNzY5N2QwNWZjMWVmYQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MDhjNGZiZjlmZDY4MTA5MWQyYTRmMWZmOTdlZGQzNmRkOTM0ZWUwNmFlNzBl
|
10
|
+
NmUxYjAzYTc4Y2ZiYzlhMzdiMTZhMDFlMDRiNTY5NmJiYTVmNjM4MjlhMmY3
|
11
|
+
ODY4ZjJkMDhhNmFkMzkwNDkwY2QzMmM5ODZiYWU3ZDFmMWUwMjM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
OGIzOGJiNDM2ZmQ4YTVhMDVjMWE1ZjMxNGI0MjY1MjI1MTgyYTI0NjJmZGY0
|
14
|
+
MWU1ODIyOWI5MmVkOTcwYTQxNTBmZDQxZWExZjcxMjNkNjAzNzc4NDQzMDJh
|
15
|
+
ZTQyZjA0OTViYjZiZTllZTNmZGU3OGE1NjZkNjkxYjQwZWY4ZTU=
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/lib/arithmetic.rb
CHANGED
@@ -10,7 +10,7 @@ module Arithmetic
|
|
10
10
|
Expression.new(Parser.new(expression).parse)
|
11
11
|
end
|
12
12
|
|
13
|
-
def self.
|
14
|
-
|
13
|
+
def self.is_a_number?(token)
|
14
|
+
Arithmetic::Parser.is_a_number?(token)
|
15
15
|
end
|
16
16
|
end
|
data/lib/arithmetic/parser.rb
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
module Arithmetic
|
2
2
|
class Parser
|
3
|
+
|
4
|
+
def self.is_a_number?(str)
|
5
|
+
str.respond_to?(:to_str) && !!str.to_str.match(/^[\d\.]+$/)
|
6
|
+
end
|
7
|
+
|
3
8
|
def initialize(exp)
|
4
9
|
@expression = exp.to_s
|
5
10
|
@node_stack = []
|
@@ -45,7 +50,7 @@ module Arithmetic
|
|
45
50
|
private
|
46
51
|
|
47
52
|
def push_operand(operand)
|
48
|
-
raise InvalidExpression.new(@expression) unless is_a_number?(operand)
|
53
|
+
raise InvalidExpression.new(@expression) unless Arithmetic::Parser.is_a_number?(operand)
|
49
54
|
@node_stack.push(OperandNode.new(operand))
|
50
55
|
end
|
51
56
|
|
@@ -60,10 +65,6 @@ module Arithmetic
|
|
60
65
|
|
61
66
|
@node_stack.push(OperatorNode.new(operator, operands))
|
62
67
|
end
|
63
|
-
|
64
|
-
def is_a_number?(str)
|
65
|
-
!!str.match(/^[\d\.]+$/)
|
66
|
-
end
|
67
68
|
end
|
68
69
|
|
69
70
|
class Tokenizer
|
data/lib/arithmetic/version.rb
CHANGED
data/spec/arithmetic_spec.rb
CHANGED
@@ -96,6 +96,20 @@ describe Arithmetic do
|
|
96
96
|
end
|
97
97
|
end
|
98
98
|
|
99
|
+
describe ".is_a_number?" do
|
100
|
+
it "returns true if argument is a number" do
|
101
|
+
expect(Arithmetic.is_a_number?('23.5')).to eq(true)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "returns false if argument is not a number" do
|
105
|
+
expect(Arithmetic.is_a_number?('2aa23')).to eq(false)
|
106
|
+
end
|
107
|
+
|
108
|
+
it "returns false if argument is not convertible to string" do
|
109
|
+
expect(Arithmetic.is_a_number?(Object.new)).to eq(false)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
99
113
|
def exp_should_error(exp)
|
100
114
|
expect {test_init exp}.to raise_error Arithmetic::InvalidExpression
|
101
115
|
end
|