little_math_pet 0.2.0 → 0.3.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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -1,9 +1,11 @@
1
1
  class LittleMathPet
2
2
  # This is used to match numbers in regular expressions
3
- NUMBER_RX = '([\+\-]?\d+(\.\d+)?)'
3
+ NUMBER_RX = '[\+\-]?\d+(?:\.\d+)?'
4
4
 
5
5
  # This is used to match mathematical signs in regular expressions
6
- SIGN_RX = '([\+\-\/\*\^])'
6
+ SIGN_RX = '[\+\-\/\*\^]'
7
+
8
+ PARENTHESES_CAPTURE_RX = /\(([^\(\)]+)\)/
7
9
 
8
10
  # The proper math order (power, multiplication/division and addiction/subtraction)
9
11
  MATH_ORDER = [%w[^], %w[* /], %w[+ -]]
@@ -60,35 +62,35 @@ class LittleMathPet
60
62
  # This is the top level method which deals with the various cases
61
63
  def do_math(math)
62
64
  case math
63
- when /\([^\)]+\)/ # match parentheses
64
- math = math.gsub(/\(([^\)]+)\)/) do |match|
65
+ when PARENTHESES_CAPTURE_RX # match parentheses
66
+ math = math.gsub(PARENTHESES_CAPTURE_RX) do |match|
65
67
  do_math($1)
66
68
  end
67
69
 
68
70
  do_math(math)
69
- when /^#{NUMBER_RX}(#{SIGN_RX}#{NUMBER_RX})+$/
71
+ when /^#{NUMBER_RX}(?:#{SIGN_RX}#{NUMBER_RX})+$/
70
72
  solve_math(math)
71
73
  when /^#{NUMBER_RX}$/
72
74
  math.to_f
73
75
  when /^#{SIGN_RX}$/
74
76
  math
75
- when ""
76
- # this appears when '-5' gets split to ['', '-', '5']
77
- 0.0
78
77
  else
79
- raise 'Invalid math expression'
78
+ raise "Invalid math expression: #{math}"
80
79
  end
81
80
  end
82
81
 
83
82
  # This is the actual solver that invokes the marh Procs
84
83
  def solve_math(math)
85
- parts = math.split(/#{SIGN_RX}/)
84
+ first_number, rest = math.scan(/^(#{NUMBER_RX})((?:#{SIGN_RX}#{NUMBER_RX})+)$/).flatten
85
+ parts = ([first_number] + rest.scan(/(#{SIGN_RX})(#{NUMBER_RX})/)).flatten
86
+ # at this point, `parts` consists of math signs and numbers in string format
86
87
 
87
88
  until parts.all?{|p| p.to_s[/^#{SIGN_RX}$/] or p.is_a?(Float)}
88
89
  parts.collect! do |part|
89
90
  do_math(part)
90
91
  end
91
92
  end
93
+ # at this point, `parts consists of math signs and numbers in float format
92
94
 
93
95
  # reduces the math expression one process at a time until we have a number
94
96
  until parts.length == 1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "little_math_pet"
8
- s.version = "0.2.0"
8
+ s.version = "0.3.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 = "2014-02-23"
12
+ s.date = "2014-10-22"
13
13
  s.description = "LittleMathPet understands simple math expressions in string format with mutliple variables and returns the result of the expression"
14
14
  s.email = "kostas.karachalios@me.com"
15
15
  s.extra_rdoc_files = [
@@ -46,6 +46,10 @@ describe LittleMathPet do
46
46
  "15/5*2" => 6.0,
47
47
  "-15/5*7" => -21.0,
48
48
  "12+10+50%+20%" => 30,
49
+ "-6 * 5" => -30,
50
+ "6 * -5" => -30,
51
+ "30 / -3" => -10,
52
+ "-30 / -3" => 10,
49
53
  }
50
54
 
51
55
  equations.each do |equation, result|
@@ -70,6 +74,19 @@ describe LittleMathPet do
70
74
  end
71
75
  end
72
76
 
77
+ context "when an equation with multiple parenthesis is given" do
78
+ equations = {
79
+ "5 - (-3 - (-8 / 2))" => 4.0,
80
+ "((7 - 2) * 6) / 2" => 15.0,
81
+ }
82
+
83
+ equations.each do |equation, result|
84
+ it "respects the proper math order (#{equation} = #{result})" do
85
+ LittleMathPet.new(equation).calc.should == equations[equation]
86
+ end
87
+ end
88
+ end
89
+
73
90
  context "when a different grammar is used for the equation" do
74
91
  equations = {
75
92
  "5*3:10" => 1.5,
@@ -86,7 +103,7 @@ describe LittleMathPet do
86
103
  end
87
104
  end
88
105
 
89
- context "when something unkown is given as math" do
106
+ context "when something unknown is given as math" do
90
107
  equations = [
91
108
  "five * 5",
92
109
  "something else",
@@ -99,7 +116,7 @@ describe LittleMathPet do
99
116
  LittleMathPet.new(equation).calc
100
117
  true.should == false
101
118
  rescue => e
102
- e.message.should == 'Invalid math expression'
119
+ e.message.should =~ /Invalid math expression/
103
120
  end
104
121
  end
105
122
  end
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: little_math_pet
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.0
5
+ version: 0.3.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kostas Karachalios
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-23 00:00:00.000000000 Z
12
+ date: 2014-10-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -95,7 +95,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
95
95
  - !ruby/object:Gem::Version
96
96
  segments:
97
97
  - 0
98
- hash: 1957235786522962572
98
+ hash: 557396944221374897
99
99
  version: '0'
100
100
  required_rubygems_version: !ruby/object:Gem::Requirement
101
101
  none: false