little_math_pet 0.1.3 → 0.2.0
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/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +5 -5
- data/README.rdoc +17 -7
- data/Rakefile +20 -20
- data/VERSION +1 -1
- data/lib/little_math_pet.rb +126 -111
- data/little_math_pet.gemspec +13 -12
- data/spec/little_math_pet_spec.rb +160 -153
- data/spec/spec_helper.rb +3 -6
- metadata +65 -52
- data/.rvmrc +0 -1
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.9.3-p484
|
data/.travis.yml
ADDED
data/Gemfile
CHANGED
@@ -5,8 +5,8 @@ source "http://rubygems.org"
|
|
5
5
|
|
6
6
|
# Add dependencies to develop your gem here.
|
7
7
|
# Include everything needed to run rake, tests, features, etc.
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
8
|
+
|
9
|
+
gem "bundler", "~> 1.0.0"
|
10
|
+
gem "jeweler", "~> 1.5.2"
|
11
|
+
gem "rspec", "~> 2.5.0"
|
12
|
+
|
data/README.rdoc
CHANGED
@@ -2,23 +2,33 @@
|
|
2
2
|
|
3
3
|
This is a simple math parser. It always returns in Float.
|
4
4
|
|
5
|
-
0.1 -
|
5
|
+
0.1 - Supports addition/subtraction, multiplication/division, exponents and brackets in their correct order.
|
6
|
+
|
7
|
+
0.2 - Supports percentage calculations.
|
6
8
|
|
7
9
|
== Examples
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
|
11
|
+
Install:
|
12
|
+
gem install little_math_pet
|
13
|
+
|
14
|
+
Require:
|
15
|
+
require "little_math_pet"
|
16
|
+
|
17
|
+
Usage:
|
18
|
+
LittleMathPet.new("5*12+8").calc # => 68.0
|
19
|
+
LittleMathPet.new("5*(12+8)").calc # => 100.0
|
20
|
+
LittleMathPet.new("60-20%").calc # => 48.0
|
21
|
+
|
12
22
|
It also can handle variables
|
13
|
-
|
14
|
-
|
23
|
+
LittleMathPet.new("5*a+8").calc(:a => 4) # => 28.0
|
24
|
+
LittleMathPet.new("a*b+c^d").calc(:a => 4, :b => 3, :c => 2, :d => 5) # => 44.0
|
15
25
|
|
16
26
|
== Syntax
|
17
27
|
|
18
28
|
Mostly ruby (+ - * / ** and ()) but can also accept : for division, ^ for exponents and [] for bracketing. It will also not mind about spaces.
|
19
29
|
|
20
30
|
== Contributing to little-math-pet
|
21
|
-
|
31
|
+
|
22
32
|
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
23
33
|
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
24
34
|
* Fork the project
|
data/Rakefile
CHANGED
@@ -1,46 +1,46 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'bundler'
|
3
3
|
begin
|
4
|
-
|
4
|
+
Bundler.setup(:default, :development)
|
5
5
|
rescue Bundler::BundlerError => e
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
9
|
end
|
10
10
|
require 'rake'
|
11
11
|
|
12
12
|
require 'jeweler'
|
13
13
|
Jeweler::Tasks.new do |gem|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "little_math_pet"
|
16
|
+
gem.homepage = "http://github.com/vrinek/little-math-pet"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{Parses math expressions and returns the result}
|
19
|
+
gem.description = %Q{LittleMathPet understands simple math expressions in string format with mutliple variables and returns the result of the expression}
|
20
|
+
gem.email = "kostas.karachalios@me.com"
|
21
|
+
gem.authors = ["Kostas Karachalios"]
|
22
22
|
end
|
23
23
|
Jeweler::RubygemsDotOrgTasks.new
|
24
24
|
|
25
25
|
require 'rake/rdoctask'
|
26
26
|
Rake::RDocTask.new do |rdoc|
|
27
|
-
|
27
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
rdoc.rdoc_dir = 'rdoc'
|
30
|
+
rdoc.title = "little-math-pet #{version}"
|
31
|
+
rdoc.rdoc_files.include('README*')
|
32
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
33
33
|
end
|
34
34
|
|
35
35
|
require 'rspec/core'
|
36
36
|
require 'rspec/core/rake_task'
|
37
37
|
RSpec::Core::RakeTask.new(:spec) do |spec|
|
38
|
-
|
38
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
39
39
|
end
|
40
40
|
|
41
41
|
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
42
|
-
|
43
|
-
|
42
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
43
|
+
spec.rcov = true
|
44
44
|
end
|
45
45
|
|
46
46
|
task :default => :spec
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/little_math_pet.rb
CHANGED
@@ -1,113 +1,128 @@
|
|
1
1
|
class LittleMathPet
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
2
|
+
# This is used to match numbers in regular expressions
|
3
|
+
NUMBER_RX = '([\+\-]?\d+(\.\d+)?)'
|
4
|
+
|
5
|
+
# This is used to match mathematical signs in regular expressions
|
6
|
+
SIGN_RX = '([\+\-\/\*\^])'
|
7
|
+
|
8
|
+
# The proper math order (power, multiplication/division and addiction/subtraction)
|
9
|
+
MATH_ORDER = [%w[^], %w[* /], %w[+ -]]
|
10
|
+
|
11
|
+
# The processes to use for the math
|
12
|
+
MATH_PROCS = {
|
13
|
+
'-' => Proc.new{|a,b| a-b},
|
14
|
+
'+' => Proc.new{|a,b| a+b},
|
15
|
+
'*' => Proc.new{|a,b| a*b},
|
16
|
+
'/' => Proc.new{|a,b| a/b},
|
17
|
+
'^' => Proc.new{|a,b| a**b}
|
18
|
+
}
|
19
|
+
|
20
|
+
def initialize(math_expression)
|
21
|
+
@math = math_expression
|
22
|
+
@math = @math.gsub(/\s/, '') # no spaces allowed
|
23
|
+
|
24
|
+
@math = @math.gsub(/\:/, '/') # 4:2 -> 4/2
|
25
|
+
@math = @math.gsub(/\*\*/, '^') # 4**2 -> 4^2 to make it easier to differantiate from 4*2
|
26
|
+
|
27
|
+
@math = @math.gsub(/\[/, '(')
|
28
|
+
@math = @math.gsub(/\]/, ')')
|
29
|
+
|
30
|
+
# If we add/subtract a percent, we multiply/divide by `1 + (percent / 100)`
|
31
|
+
@math.gsub!(/(\+|\-)(#{NUMBER_RX})%/) do |match|
|
32
|
+
case $1
|
33
|
+
when "+"
|
34
|
+
"*#{1 + ($2.to_f / 100)}"
|
35
|
+
when "-"
|
36
|
+
"*#{1 - ($2.to_f / 100)}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# All other percents: just divide by 100
|
41
|
+
@math.gsub!(/(#{NUMBER_RX})%/) do |num|
|
42
|
+
$1.to_f / 100
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def calc(variables = {})
|
47
|
+
unless variables.empty?
|
48
|
+
variables.each do |letter, number|
|
49
|
+
@math.gsub!(letter.to_s, number.to_s)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
raise 'Invalid math expression' unless @math[/^[\d\-\+\/\*\^\.\(\)]+$/]
|
54
|
+
|
55
|
+
do_math(@math)
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
# This is the top level method which deals with the various cases
|
61
|
+
def do_math(math)
|
62
|
+
case math
|
63
|
+
when /\([^\)]+\)/ # match parentheses
|
64
|
+
math = math.gsub(/\(([^\)]+)\)/) do |match|
|
65
|
+
do_math($1)
|
66
|
+
end
|
67
|
+
|
68
|
+
do_math(math)
|
69
|
+
when /^#{NUMBER_RX}(#{SIGN_RX}#{NUMBER_RX})+$/
|
70
|
+
solve_math(math)
|
71
|
+
when /^#{NUMBER_RX}$/
|
72
|
+
math.to_f
|
73
|
+
when /^#{SIGN_RX}$/
|
74
|
+
math
|
75
|
+
when ""
|
76
|
+
# this appears when '-5' gets split to ['', '-', '5']
|
77
|
+
0.0
|
78
|
+
else
|
79
|
+
raise 'Invalid math expression'
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# This is the actual solver that invokes the marh Procs
|
84
|
+
def solve_math(math)
|
85
|
+
parts = math.split(/#{SIGN_RX}/)
|
86
|
+
|
87
|
+
until parts.all?{|p| p.to_s[/^#{SIGN_RX}$/] or p.is_a?(Float)}
|
88
|
+
parts.collect! do |part|
|
89
|
+
do_math(part)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# reduces the math expression one process at a time until we have a number
|
94
|
+
until parts.length == 1
|
95
|
+
i = 0
|
96
|
+
proc_positions = parts.inject({}) do |hash, part|
|
97
|
+
if part.to_s[/^#{SIGN_RX}$/]
|
98
|
+
hash[part] ||= i
|
99
|
+
end
|
100
|
+
i += 1
|
101
|
+
|
102
|
+
hash
|
103
|
+
end
|
104
|
+
|
105
|
+
next_proc = nil
|
106
|
+
next_position = nil
|
107
|
+
MATH_ORDER.each do |symbols|
|
108
|
+
unless (symbols & proc_positions.keys).empty? or next_proc
|
109
|
+
next_proc = (symbols & proc_positions.keys).min_by do |p|
|
110
|
+
proc_positions[p]
|
111
|
+
end
|
112
|
+
next_position = proc_positions[next_proc]
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
if parts[next_position][/^#{SIGN_RX}$/]
|
117
|
+
result = MATH_PROCS[parts[next_position]].call((parts[next_position-1] || 0), parts[next_position+1])
|
118
|
+
parts[next_position-1] = parts[next_position+1] = nil
|
119
|
+
parts[next_position] = result
|
120
|
+
parts.compact!
|
121
|
+
else
|
122
|
+
raise 'Invalid math expression'
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
return parts.first
|
127
|
+
end
|
113
128
|
end
|
data/little_math_pet.gemspec
CHANGED
@@ -4,21 +4,22 @@
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "0.
|
7
|
+
s.name = "little_math_pet"
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Kostas Karachalios"]
|
12
|
-
s.date =
|
13
|
-
s.description =
|
14
|
-
s.email =
|
12
|
+
s.date = "2014-02-23"
|
13
|
+
s.description = "LittleMathPet understands simple math expressions in string format with mutliple variables and returns the result of the expression"
|
14
|
+
s.email = "kostas.karachalios@me.com"
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE.txt",
|
17
17
|
"README.rdoc"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
|
-
".
|
21
|
+
".ruby-version",
|
22
|
+
".travis.yml",
|
22
23
|
"Gemfile",
|
23
24
|
"Gemfile.lock",
|
24
25
|
"LICENSE.txt",
|
@@ -30,11 +31,11 @@ Gem::Specification.new do |s|
|
|
30
31
|
"spec/little_math_pet_spec.rb",
|
31
32
|
"spec/spec_helper.rb"
|
32
33
|
]
|
33
|
-
s.homepage =
|
34
|
+
s.homepage = "http://github.com/vrinek/little-math-pet"
|
34
35
|
s.licenses = ["MIT"]
|
35
36
|
s.require_paths = ["lib"]
|
36
|
-
s.rubygems_version =
|
37
|
-
s.summary =
|
37
|
+
s.rubygems_version = "1.8.23"
|
38
|
+
s.summary = "Parses math expressions and returns the result"
|
38
39
|
s.test_files = [
|
39
40
|
"spec/little_math_pet_spec.rb",
|
40
41
|
"spec/spec_helper.rb"
|
@@ -44,9 +45,9 @@ Gem::Specification.new do |s|
|
|
44
45
|
s.specification_version = 3
|
45
46
|
|
46
47
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
47
|
-
s.
|
48
|
-
s.
|
49
|
-
s.
|
48
|
+
s.add_runtime_dependency(%q<bundler>, ["~> 1.0.0"])
|
49
|
+
s.add_runtime_dependency(%q<jeweler>, ["~> 1.5.2"])
|
50
|
+
s.add_runtime_dependency(%q<rspec>, ["~> 2.5.0"])
|
50
51
|
else
|
51
52
|
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
52
53
|
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
@@ -1,157 +1,164 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
describe LittleMathPet do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
4
|
+
context "when a single number is given" do
|
5
|
+
equations = {
|
6
|
+
"8" => 8.0,
|
7
|
+
"78.65" => 78.65,
|
8
|
+
"-3.5" => -3.5,
|
9
|
+
"30%" => 0.3
|
10
|
+
}
|
11
|
+
|
12
|
+
equations.each do |equation, result|
|
13
|
+
it "returns that number in float (#{equation} = #{result})" do
|
14
|
+
LittleMathPet.new(equation).calc.should == result
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context "when a simple equation is given" do
|
20
|
+
equations = {
|
21
|
+
"5+3" => 8.0,
|
22
|
+
"7-5" => 2.0,
|
23
|
+
"5-7" => -2.0,
|
24
|
+
"5*3" => 15.0,
|
25
|
+
"15/5" => 3.0,
|
26
|
+
"-15/5" => -3.0,
|
27
|
+
"2^3" => 8.0,
|
28
|
+
"12+50%" => 18.0,
|
29
|
+
"120*25%" => 30.0,
|
30
|
+
"7 / 50%" => 14.0
|
31
|
+
}
|
32
|
+
|
33
|
+
equations.each do |equation, result|
|
34
|
+
it "solves the equation (#{equation} = #{result})" do
|
35
|
+
LittleMathPet.new(equation).calc.should == result
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "when a more complex equation is given" do
|
41
|
+
equations = {
|
42
|
+
"5+3+10" => 18.0,
|
43
|
+
"7-5-2" => 0.0,
|
44
|
+
"5-7+6" => 4.0,
|
45
|
+
"5*3/10" => 1.5,
|
46
|
+
"15/5*2" => 6.0,
|
47
|
+
"-15/5*7" => -21.0,
|
48
|
+
"12+10+50%+20%" => 30,
|
49
|
+
}
|
50
|
+
|
51
|
+
equations.each do |equation, result|
|
52
|
+
it "solves the equation (#{equation} = #{result})" do
|
53
|
+
LittleMathPet.new(equation).calc.should == result
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
equations = {
|
58
|
+
"5*3+10" => 25.0,
|
59
|
+
"10+3*5" => 25.0,
|
60
|
+
"5-7/2" => 1.5,
|
61
|
+
"-8/2^2" => -2.0,
|
62
|
+
"(5+2)*2" => 14.0,
|
63
|
+
"(5+2)+50%" => 10.5,
|
64
|
+
}
|
65
|
+
|
66
|
+
equations.each do |equation, result|
|
67
|
+
it "respects the proper math order (#{equation} = #{result})" do
|
68
|
+
LittleMathPet.new(equation).calc.should == equations[equation]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context "when a different grammar is used for the equation" do
|
74
|
+
equations = {
|
75
|
+
"5*3:10" => 1.5,
|
76
|
+
"-8/2**2" => -2.0,
|
77
|
+
"5 + 7" => 12.0,
|
78
|
+
"[5+2] : 2" => 3.5,
|
79
|
+
"100 + 40 %" => 140,
|
80
|
+
}
|
81
|
+
|
82
|
+
equations.each do |equation, result|
|
83
|
+
it "solves the equation (#{equation} = #{result})" do
|
84
|
+
LittleMathPet.new(equation).calc.should == result
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context "when something unkown is given as math" do
|
90
|
+
equations = [
|
91
|
+
"five * 5",
|
92
|
+
"something else",
|
93
|
+
"+*+"
|
94
|
+
]
|
95
|
+
|
96
|
+
equations.each do |equation|
|
97
|
+
it "raises an exception (#{equation.inspect})" do
|
98
|
+
begin
|
99
|
+
LittleMathPet.new(equation).calc
|
100
|
+
true.should == false
|
101
|
+
rescue => e
|
102
|
+
e.message.should == 'Invalid math expression'
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context "when a variable is given (x => 3)" do
|
109
|
+
value = '3'
|
110
|
+
equations = {
|
111
|
+
"5+x+10" => 18.0,
|
112
|
+
"7-5-x" => -1.0,
|
113
|
+
"5*x/10" => 1.5,
|
114
|
+
"15/5*x" => 9.0,
|
115
|
+
"10+x*5" => 25.0,
|
116
|
+
"x-7/2" => -0.5,
|
117
|
+
"-9/x^2" => -1.0,
|
118
|
+
"x*(2+3)/2" => 7.5
|
119
|
+
}
|
120
|
+
|
121
|
+
equations.each do |equation, result|
|
122
|
+
it "uses it in place of x (#{equation} = #{result})" do
|
123
|
+
LittleMathPet.new(equation).calc(:x => value).should == result
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
context "when multiple variables are given (a => 2, b => 5)" do
|
129
|
+
values = {:a => 2, :b => 5}
|
130
|
+
equations = {
|
131
|
+
"5+a+10/b" => 9.0,
|
132
|
+
"7-5-a*b" => -8.0,
|
133
|
+
"5^a/10/b" => 0.5,
|
134
|
+
"b*(a+3)/2" => 12.5
|
135
|
+
}
|
136
|
+
|
137
|
+
equations.each do |equation, result|
|
138
|
+
it "uses them in place of the variables (#{equation} = #{result})" do
|
139
|
+
LittleMathPet.new(equation).calc(values).should == result
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
context "when some variables are missing" do
|
145
|
+
values = {:a => 2, :b => 5}
|
146
|
+
equations = [
|
147
|
+
"5+a+10/x",
|
148
|
+
"7-5-a*y",
|
149
|
+
"c^a/10/b",
|
150
|
+
"v*(e+3)/2"
|
151
|
+
]
|
152
|
+
|
153
|
+
equations.each do |equation, result|
|
154
|
+
it "raises an exception (#{equation.inspect})" do
|
155
|
+
begin
|
156
|
+
LittleMathPet.new(equation).calc.should == result
|
157
|
+
true.should == false
|
158
|
+
rescue => e
|
159
|
+
e.message.should == 'Invalid math expression'
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
157
164
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,12 +1,9 @@
|
|
1
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
1
|
require 'rspec'
|
4
|
-
|
2
|
+
|
3
|
+
require File.expand_path('../../lib/little_math_pet.rb', __FILE__)
|
5
4
|
|
6
5
|
# Requires supporting files with custom matchers and macros, etc,
|
7
6
|
# in ./support/ and its subdirectories.
|
8
7
|
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
8
|
|
10
|
-
RSpec.configure do |config
|
11
|
-
|
12
|
-
end
|
9
|
+
RSpec.configure do |config|; end
|
metadata
CHANGED
@@ -1,63 +1,76 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: little_math_pet
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.2.0
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Kostas Karachalios
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
dependencies:
|
16
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2014-02-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
17
15
|
name: bundler
|
18
|
-
|
19
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
17
|
none: false
|
21
|
-
requirements:
|
18
|
+
requirements:
|
22
19
|
- - ~>
|
23
|
-
- !ruby/object:Gem::Version
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.0.0
|
22
|
+
requirement: !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
24
27
|
version: 1.0.0
|
25
|
-
type: :development
|
26
|
-
version_requirements: *id001
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: jeweler
|
29
28
|
prerelease: false
|
30
|
-
|
29
|
+
type: :runtime
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: jeweler
|
32
|
+
version_requirements: !ruby/object:Gem::Requirement
|
31
33
|
none: false
|
32
|
-
requirements:
|
34
|
+
requirements:
|
33
35
|
- - ~>
|
34
|
-
- !ruby/object:Gem::Version
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 1.5.2
|
38
|
+
requirement: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
35
43
|
version: 1.5.2
|
36
|
-
type: :development
|
37
|
-
version_requirements: *id002
|
38
|
-
- !ruby/object:Gem::Dependency
|
39
|
-
name: rspec
|
40
44
|
prerelease: false
|
41
|
-
|
45
|
+
type: :runtime
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
49
|
none: false
|
43
|
-
requirements:
|
50
|
+
requirements:
|
44
51
|
- - ~>
|
45
|
-
- !ruby/object:Gem::Version
|
52
|
+
- !ruby/object:Gem::Version
|
46
53
|
version: 2.5.0
|
47
|
-
|
48
|
-
|
49
|
-
|
54
|
+
requirement: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ~>
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: 2.5.0
|
60
|
+
prerelease: false
|
61
|
+
type: :runtime
|
62
|
+
description: LittleMathPet understands simple math expressions in string format with
|
63
|
+
mutliple variables and returns the result of the expression
|
50
64
|
email: kostas.karachalios@me.com
|
51
65
|
executables: []
|
52
|
-
|
53
66
|
extensions: []
|
54
|
-
|
55
|
-
extra_rdoc_files:
|
67
|
+
extra_rdoc_files:
|
56
68
|
- LICENSE.txt
|
57
69
|
- README.rdoc
|
58
|
-
files:
|
70
|
+
files:
|
59
71
|
- .document
|
60
|
-
- .
|
72
|
+
- .ruby-version
|
73
|
+
- .travis.yml
|
61
74
|
- Gemfile
|
62
75
|
- Gemfile.lock
|
63
76
|
- LICENSE.txt
|
@@ -68,34 +81,34 @@ files:
|
|
68
81
|
- little_math_pet.gemspec
|
69
82
|
- spec/little_math_pet_spec.rb
|
70
83
|
- spec/spec_helper.rb
|
71
|
-
has_rdoc: true
|
72
84
|
homepage: http://github.com/vrinek/little-math-pet
|
73
|
-
licenses:
|
85
|
+
licenses:
|
74
86
|
- MIT
|
75
87
|
post_install_message:
|
76
88
|
rdoc_options: []
|
77
|
-
|
78
|
-
require_paths:
|
89
|
+
require_paths:
|
79
90
|
- lib
|
80
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
92
|
none: false
|
82
|
-
requirements:
|
83
|
-
- -
|
84
|
-
- !ruby/object:Gem::Version
|
85
|
-
|
86
|
-
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
segments:
|
97
|
+
- 0
|
98
|
+
hash: 1957235786522962572
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
101
|
none: false
|
88
|
-
requirements:
|
89
|
-
- -
|
90
|
-
- !ruby/object:Gem::Version
|
91
|
-
version:
|
102
|
+
requirements:
|
103
|
+
- - '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
92
106
|
requirements: []
|
93
|
-
|
94
107
|
rubyforge_project:
|
95
|
-
rubygems_version: 1.
|
108
|
+
rubygems_version: 1.8.23
|
96
109
|
signing_key:
|
97
110
|
specification_version: 3
|
98
111
|
summary: Parses math expressions and returns the result
|
99
|
-
test_files:
|
112
|
+
test_files:
|
100
113
|
- spec/little_math_pet_spec.rb
|
101
114
|
- spec/spec_helper.rb
|
data/.rvmrc
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
rvm 1.9.2-p0@LittleMathPet
|