little_math_pet 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/little_math_pet.rb +7 -0
- data/little_math_pet.gemspec +2 -2
- data/spec/little_math_pet_spec.rb +17 -0
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/lib/little_math_pet.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
class LittleMathPet
|
2
3
|
# This is used to match numbers in regular expressions
|
3
4
|
NUMBER_RX = '[\+\-]?\d+(?:\.\d+)?'
|
@@ -23,12 +24,18 @@ class LittleMathPet
|
|
23
24
|
@math = math_expression
|
24
25
|
@math = @math.gsub(/\s/, '') # no spaces allowed
|
25
26
|
|
27
|
+
@math = @math.gsub(/\÷/, '/') # 4÷2 -> 4/2
|
26
28
|
@math = @math.gsub(/\:/, '/') # 4:2 -> 4/2
|
27
29
|
@math = @math.gsub(/\*\*/, '^') # 4**2 -> 4^2 to make it easier to differantiate from 4*2
|
28
30
|
|
31
|
+
@math = @math.gsub(/\×/, '*') # 4×2 -> 4*2
|
32
|
+
|
29
33
|
@math = @math.gsub(/\[/, '(')
|
30
34
|
@math = @math.gsub(/\]/, ')')
|
31
35
|
|
36
|
+
@math = @math.gsub(/\{/, '(')
|
37
|
+
@math = @math.gsub(/\}/, ')')
|
38
|
+
|
32
39
|
# If we add/subtract a percent, we multiply/divide by `1 + (percent / 100)`
|
33
40
|
@math.gsub!(/(\+|\-)(#{NUMBER_RX})%/) do |match|
|
34
41
|
case $1
|
data/little_math_pet.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "little_math_pet"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.4.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 = "
|
12
|
+
s.date = "2015-01-08"
|
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 = [
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
3
|
|
3
4
|
describe LittleMathPet do
|
@@ -94,6 +95,22 @@ describe LittleMathPet do
|
|
94
95
|
"5 + 7" => 12.0,
|
95
96
|
"[5+2] : 2" => 3.5,
|
96
97
|
"100 + 40 %" => 140,
|
98
|
+
"12 ÷ 4 + 5" => 8,
|
99
|
+
"2 × 8 - 7" => 9,
|
100
|
+
"2 × [8 - 7]" => 2,
|
101
|
+
}
|
102
|
+
|
103
|
+
equations.each do |equation, result|
|
104
|
+
it "solves the equation (#{equation} = #{result})" do
|
105
|
+
LittleMathPet.new(equation).calc.should == result
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context "when using curly brackets" do
|
111
|
+
equations = {
|
112
|
+
"{5*3}:10" => 1.5,
|
113
|
+
"-8/{2**2}" => -2.0,
|
97
114
|
}
|
98
115
|
|
99
116
|
equations.each do |equation, result|
|
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.
|
5
|
+
version: 0.4.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:
|
12
|
+
date: 2015-01-08 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:
|
98
|
+
hash: -4137418140742752699
|
99
99
|
version: '0'
|
100
100
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
101
|
none: false
|