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 ADDED
@@ -0,0 +1 @@
1
+ 1.9.3-p484
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
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
- group :development do
9
- gem "bundler", "~> 1.0.0"
10
- gem "jeweler", "~> 1.5.2"
11
- gem "rspec", "~> 2.5.0"
12
- end
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 - It supports addition/subtraction, multiplication/division, exponents and brackets in their correct order.
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
- LittleMathPet.new("5*12+8").calc # => 68.0
10
- LittleMathPet.new("5*(12+8)").calc # => 100.0
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
- LittleMathPet.new("5*a+8").calc(:a => 4) # => 28.0
14
- LittleMathPet.new("a*b+c^d").calc(:a => 4, :b => 3, :c => 2, :d => 5) # => 44.0
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
- Bundler.setup(:default, :development)
4
+ Bundler.setup(:default, :development)
5
5
  rescue Bundler::BundlerError => e
6
- $stderr.puts e.message
7
- $stderr.puts "Run `bundle install` to install missing gems"
8
- exit e.status_code
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
- # 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"]
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
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
27
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
28
28
 
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')
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
- spec.pattern = FileList['spec/**/*_spec.rb']
38
+ spec.pattern = FileList['spec/**/*_spec.rb']
39
39
  end
40
40
 
41
41
  RSpec::Core::RakeTask.new(:rcov) do |spec|
42
- spec.pattern = 'spec/**/*_spec.rb'
43
- spec.rcov = true
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.3
1
+ 0.2.0
@@ -1,113 +1,128 @@
1
1
  class LittleMathPet
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
- end
30
-
31
- def calc(variables = {})
32
- unless variables.empty?
33
- variables.each do |letter, number|
34
- @math.gsub!(letter.to_s, number.to_s)
35
- end
36
- end
37
-
38
- raise 'Invalid math expression' unless @math[/^[\d\-\+\/\*\^\.\(\)]+$/]
39
-
40
- do_math(@math)
41
- end
42
-
43
- private
44
-
45
- # This is the top level method which deals with the various cases
46
- def do_math(math)
47
- case math
48
- when /\([^\)]+\)/
49
- math = math.gsub(/\(([^\)]+)\)/) do |match|
50
- do_math($1)
51
- end
52
-
53
- do_math(math)
54
- when /^#{NUMBER_RX}(#{SIGN_RX}#{NUMBER_RX})+$/
55
- solve_math(math)
56
- when /^#{NUMBER_RX}$/
57
- math.to_f
58
- when /^#{SIGN_RX}$/
59
- math
60
- when ""
61
- # this appears when '-5' gets split to ['', '-', '5']
62
- 0.0
63
- else
64
- raise 'Invalid math expression'
65
- end
66
- end
67
-
68
- # This is the actual solver that invokes the marh Procs
69
- def solve_math(math)
70
- parts = math.split(/#{SIGN_RX}/)
71
-
72
- until parts.all?{|p| p.to_s[/^#{SIGN_RX}$/] or p.is_a?(Float)}
73
- parts.collect! do |part|
74
- do_math(part)
75
- end
76
- end
77
-
78
- # reduces the math expression one process at a time until we have a number
79
- until parts.length == 1
80
- i = 0
81
- proc_positions = parts.inject({}) do |hash, part|
82
- if part.to_s[/^#{SIGN_RX}$/]
83
- hash[part] ||= i
84
- end
85
- i += 1
86
-
87
- hash
88
- end
89
-
90
- next_proc = nil
91
- next_position = nil
92
- MATH_ORDER.each do |symbols|
93
- unless (symbols & proc_positions.keys).empty? or next_proc
94
- next_proc = (symbols & proc_positions.keys).min_by do |p|
95
- proc_positions[p]
96
- end
97
- next_position = proc_positions[next_proc]
98
- end
99
- end
100
-
101
- if parts[next_position][/^#{SIGN_RX}$/]
102
- result = MATH_PROCS[parts[next_position]].call((parts[next_position-1] || 0), parts[next_position+1])
103
- parts[next_position-1] = parts[next_position+1] = nil
104
- parts[next_position] = result
105
- parts.compact!
106
- else
107
- raise 'Invalid math expression'
108
- end
109
- end
110
-
111
- return parts.first
112
- end
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
@@ -4,21 +4,22 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{little_math_pet}
8
- s.version = "0.1.3"
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 = %q{2011-02-15}
13
- s.description = %q{LittleMathPet understands simple math expressions in string format with mutliple variables and returns the result of the expression}
14
- s.email = %q{kostas.karachalios@me.com}
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
- ".rvmrc",
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 = %q{http://github.com/vrinek/little-math-pet}
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 = %q{1.5.2}
37
- s.summary = %q{Parses math expressions and returns the result}
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.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
48
- s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
49
- s.add_development_dependency(%q<rspec>, ["~> 2.5.0"])
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
- 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
- }
10
-
11
- equations.each do |equation, result|
12
- it "returns that number in float (#{equation} = #{result})" do
13
- LittleMathPet.new(equation).calc.should == result
14
- end
15
- end
16
- end
17
-
18
- context "when a simple equation is given" do
19
- equations = {
20
- "5+3" => 8.0,
21
- "7-5" => 2.0,
22
- "5-7" => -2.0,
23
- "5*3" => 15.0,
24
- "15/5" => 3.0,
25
- "-15/5" => -3.0,
26
- "2^3" => 8.0
27
- }
28
-
29
- equations.each do |equation, result|
30
- it "solves the equation (#{equation} = #{result})" do
31
- LittleMathPet.new(equation).calc.should == result
32
- end
33
- end
34
- end
35
-
36
- context "when a more complex equation is given" do
37
- equations = {
38
- "5+3+10" => 18.0,
39
- "7-5-2" => 0.0,
40
- "5-7+6" => 4.0,
41
- "5*3/10" => 1.5,
42
- "15/5*2" => 6.0,
43
- "-15/5*7" => -21.0
44
- }
45
-
46
- equations.each do |equation, result|
47
- it "solves the equation (#{equation} = #{result})" do
48
- LittleMathPet.new(equation).calc.should == result
49
- end
50
- end
51
-
52
- equations = {
53
- "5*3+10" => 25.0,
54
- "10+3*5" => 25.0,
55
- "5-7/2" => 1.5,
56
- "-8/2^2" => -2.0,
57
- "(5+2)*2" => 14.0
58
- }
59
-
60
- equations.each do |equation, result|
61
- it "respects the proper math order (#{equation} = #{result})" do
62
- LittleMathPet.new(equation).calc.should == equations[equation]
63
- end
64
- end
65
- end
66
-
67
- context "when a different grammar is used for the equation" do
68
- equations = {
69
- "5*3:10" => 1.5,
70
- "-8/2**2" => -2.0,
71
- "5 + 7" => 12.0,
72
- "[5+2] : 2" => 3.5
73
- }
74
-
75
- equations.each do |equation, result|
76
- it "solves the equation (#{equation} = #{result})" do
77
- LittleMathPet.new(equation).calc.should == result
78
- end
79
- end
80
- end
81
-
82
- context "when something unkown is given as math" do
83
- equations = [
84
- "five * 5",
85
- "something else",
86
- "+*+"
87
- ]
88
-
89
- equations.each do |equation|
90
- it "raises an exception (#{equation.inspect})" do
91
- begin
92
- LittleMathPet.new(equation).calc
93
- true.should == false
94
- rescue => e
95
- e.message.should == 'Invalid math expression'
96
- end
97
- end
98
- end
99
- end
100
-
101
- context "when a variable is given (x => 3)" do
102
- value = '3'
103
- equations = {
104
- "5+x+10" => 18.0,
105
- "7-5-x" => -1.0,
106
- "5*x/10" => 1.5,
107
- "15/5*x" => 9.0,
108
- "10+x*5" => 25.0,
109
- "x-7/2" => -0.5,
110
- "-9/x^2" => -1.0,
111
- "x*(2+3)/2" => 7.5
112
- }
113
-
114
- equations.each do |equation, result|
115
- it "uses it in place of x (#{equation} = #{result})" do
116
- LittleMathPet.new(equation).calc(:x => value).should == result
117
- end
118
- end
119
- end
120
-
121
- context "when multiple variables are given (a => 2, b => 5)" do
122
- values = {:a => 2, :b => 5}
123
- equations = {
124
- "5+a+10/b" => 9.0,
125
- "7-5-a*b" => -8.0,
126
- "5^a/10/b" => 0.5,
127
- "b*(a+3)/2" => 12.5
128
- }
129
-
130
- equations.each do |equation, result|
131
- it "uses them in place of the variables (#{equation} = #{result})" do
132
- LittleMathPet.new(equation).calc(values).should == result
133
- end
134
- end
135
- end
136
-
137
- context "when some variables are missing" do
138
- values = {:a => 2, :b => 5}
139
- equations = [
140
- "5+a+10/x",
141
- "7-5-a*y",
142
- "c^a/10/b",
143
- "v*(e+3)/2"
144
- ]
145
-
146
- equations.each do |equation, result|
147
- it "raises an exception (#{equation.inspect})" do
148
- begin
149
- LittleMathPet.new(equation).calc.should == result
150
- true.should == false
151
- rescue => e
152
- e.message.should == 'Invalid math expression'
153
- end
154
- end
155
- end
156
- end
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
- require 'the-perfect-gem'
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.1.3
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
- date: 2011-02-15 00:00:00 +02:00
14
- default_executable:
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
- prerelease: false
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
- requirement: &id002 !ruby/object:Gem::Requirement
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
- requirement: &id003 !ruby/object:Gem::Requirement
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
- type: :development
48
- version_requirements: *id003
49
- description: LittleMathPet understands simple math expressions in string format with mutliple variables and returns the result of the expression
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
- - .rvmrc
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
- version: "0"
86
- required_rubygems_version: !ruby/object:Gem::Requirement
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: "0"
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
92
106
  requirements: []
93
-
94
107
  rubyforge_project:
95
- rubygems_version: 1.5.2
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