eqn 1.4.2 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +48 -32
- data/eqn.gemspec +7 -3
- data/lib/eqn/calculator.rb +4 -0
- data/lib/eqn/version.rb +1 -1
- metadata +21 -10
- data/.rspec +0 -3
- data/.rubocop.yml +0 -21
- data/.ruby-version +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3929425e71b09257fcf386a38166a5fcf2adc7e5
|
4
|
+
data.tar.gz: 2d47d23b59c716ca5adb87cfdaaa206d3aa5b17f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c74b42add0068a547fa8b547fdb4c7a342067747bde79791e89996816205c2ff1655ca60a53c184f38d6b04cb78b4229a4f079ef8d8c4457a26a8285833fade
|
7
|
+
data.tar.gz: da9ae663707fa4b07c41b60b5b87c9c3d82ed8ce530a98d7c87008afd4b81ccaa3974be56ff30c014540717424ab899a8cf45a91a4b3d1c6b27ef31515a209a1
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -18,30 +18,40 @@ gem 'eqn'
|
|
18
18
|
|
19
19
|
And then execute:
|
20
20
|
|
21
|
-
|
21
|
+
```bash
|
22
|
+
$ bundle
|
23
|
+
```
|
22
24
|
|
23
25
|
Or install it yourself as:
|
24
26
|
|
25
|
-
|
27
|
+
```bash
|
28
|
+
$ gem install eqn
|
29
|
+
```
|
26
30
|
|
27
31
|
## Usage
|
28
32
|
|
29
33
|
To evaluate an equation string, run the following:
|
30
34
|
|
31
|
-
|
32
|
-
|
35
|
+
```ruby
|
36
|
+
Eqn::Calculator.calc('1 + 1')
|
37
|
+
# => 2
|
38
|
+
```
|
33
39
|
|
34
40
|
You can also check if an equation is valid:
|
35
41
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
42
|
+
```ruby
|
43
|
+
Eqn::Calculator.valid?('1 + 1')
|
44
|
+
# => true
|
45
|
+
Eqn::Calculator.valid?('1 + / 1')
|
46
|
+
# => false
|
47
|
+
```
|
40
48
|
|
41
49
|
If you want to peek at how Eqn is parsing an equation, run the following to get the syntax tree:
|
42
50
|
|
43
|
-
|
44
|
-
|
51
|
+
```ruby
|
52
|
+
Eqn::Parser.parse('1 + 1')
|
53
|
+
# => <syntax tree is printed>
|
54
|
+
```
|
45
55
|
|
46
56
|
Eqn follows the standard mathematical order of operations: parentheses, exponentiation, multiplication/division, addition/subtraction. It ignores whitespace, so `1 + 1` === `1+1`. (However, it does not ignore whitespace between two numbers, so `1 1` is invalid.)
|
47
57
|
|
@@ -49,36 +59,42 @@ Eqn follows the standard mathematical order of operations: parentheses, exponent
|
|
49
59
|
|
50
60
|
Eqn supports dynamically inserting values into an equation. Variables are passed to the calculator method via a hash; variable names may contain any lowercase or uppercase letters. Some examples:
|
51
61
|
|
52
|
-
|
53
|
-
|
62
|
+
```ruby
|
63
|
+
Eqn::Calculator.calc('a + 1', a: 1)
|
64
|
+
# => 2
|
54
65
|
|
55
|
-
|
56
|
-
|
66
|
+
Eqn::Calculator.calc('5 * value', value: 2.5)
|
67
|
+
# => 12.5
|
57
68
|
|
58
|
-
|
59
|
-
|
69
|
+
Eqn::Calculator.calc('if(a > 10, b, c)', a: 15, b: 1, c: 0) # see below for function documentation
|
70
|
+
# => 1
|
71
|
+
```
|
60
72
|
|
61
73
|
If you need distinct equations with variable sets, you can instantiate separate instances:
|
62
74
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
75
|
+
```ruby
|
76
|
+
calc = Eqn::Calculator.new('1 + abc', abc: 2.0)
|
77
|
+
calc.calc
|
78
|
+
# => 3.0
|
79
|
+
calc_two = Eqn::Calculator.new('1 + abc', abc: 5.0)
|
80
|
+
calc_two.calc
|
81
|
+
# => 6.0
|
82
|
+
```
|
69
83
|
|
70
84
|
On calculator instances, variables can be set via key-value syntax, hash syntax, or as a method:
|
71
85
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
86
|
+
```ruby
|
87
|
+
calc = Eqn::Calculator.new('1 + abc')
|
88
|
+
calc.set(:abc, 3.0)
|
89
|
+
calc.abc
|
90
|
+
# => 3.0
|
91
|
+
calc.set(abc: 4.0)
|
92
|
+
calc.abc
|
93
|
+
# => 4.0
|
94
|
+
calc.abc = 5.0
|
95
|
+
calc.abc
|
96
|
+
# => 5.0
|
97
|
+
```
|
82
98
|
|
83
99
|
### Functions
|
84
100
|
|
data/eqn.gemspec
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
# coding: utf-8
|
2
|
+
|
2
3
|
lib = File.expand_path('../lib', __FILE__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
5
|
require 'eqn/version'
|
5
6
|
|
7
|
+
config_files = %w(.rspec .rubocop.yml .ruby-version Appraisals circle.yml)
|
8
|
+
|
6
9
|
Gem::Specification.new do |spec|
|
7
10
|
spec.name = 'eqn'
|
8
11
|
spec.version = Eqn::VERSION
|
@@ -14,17 +17,18 @@ Gem::Specification.new do |spec|
|
|
14
17
|
spec.homepage = 'https://github.com/schneidmaster/eqn'
|
15
18
|
spec.license = 'MIT'
|
16
19
|
|
17
|
-
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| config_files.include?(f) || f.match(%r{^(spec|gemfiles)/}) }
|
18
21
|
spec.bindir = 'exe'
|
19
22
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
23
|
spec.require_paths = ['lib']
|
21
24
|
|
22
|
-
spec.add_dependency 'treetop', '
|
25
|
+
spec.add_dependency 'treetop', '>= 1.2.0'
|
23
26
|
|
27
|
+
spec.add_development_dependency 'appraisal', '~> 2.1.0'
|
24
28
|
spec.add_development_dependency 'bundler', '~> 1.8'
|
25
29
|
spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.4.7'
|
26
30
|
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
-
spec.add_development_dependency 'rubocop', '~> 0.
|
31
|
+
spec.add_development_dependency 'rubocop', '~> 0.42'
|
28
32
|
spec.add_development_dependency 'rspec', '~> 3.2.0'
|
29
33
|
spec.add_development_dependency 'simplecov', '~> 0.10.0'
|
30
34
|
end
|
data/lib/eqn/calculator.rb
CHANGED
@@ -25,6 +25,10 @@ module Eqn
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
+
def respond_to_missing?(method)
|
29
|
+
%i(calculate calc valid?).include?(method) || method.to_s.match(/^[A-Za-z]+=$/) || (var = method.to_s.match(/^[A-Za-z]+$/).to_s.intern) && @vars.key?(var)
|
30
|
+
end
|
31
|
+
|
28
32
|
class << self
|
29
33
|
def calculate(data, vars = {})
|
30
34
|
@@vars = vars
|
data/lib/eqn/version.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eqn
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zach Schneider
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-03-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: treetop
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 1.
|
19
|
+
version: 1.2.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: appraisal
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.1.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
23
37
|
requirements:
|
24
38
|
- - "~>"
|
25
39
|
- !ruby/object:Gem::Version
|
26
|
-
version: 1.
|
40
|
+
version: 2.1.0
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -72,14 +86,14 @@ dependencies:
|
|
72
86
|
requirements:
|
73
87
|
- - "~>"
|
74
88
|
- !ruby/object:Gem::Version
|
75
|
-
version: '0.
|
89
|
+
version: '0.42'
|
76
90
|
type: :development
|
77
91
|
prerelease: false
|
78
92
|
version_requirements: !ruby/object:Gem::Requirement
|
79
93
|
requirements:
|
80
94
|
- - "~>"
|
81
95
|
- !ruby/object:Gem::Version
|
82
|
-
version: '0.
|
96
|
+
version: '0.42'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: rspec
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -117,9 +131,6 @@ extensions: []
|
|
117
131
|
extra_rdoc_files: []
|
118
132
|
files:
|
119
133
|
- ".gitignore"
|
120
|
-
- ".rspec"
|
121
|
-
- ".rubocop.yml"
|
122
|
-
- ".ruby-version"
|
123
134
|
- Gemfile
|
124
135
|
- LICENSE.txt
|
125
136
|
- README.md
|
data/.rspec
DELETED
data/.rubocop.yml
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
AllCops:
|
2
|
-
Exclude:
|
3
|
-
- db/**/*
|
4
|
-
- vendor/**/*
|
5
|
-
|
6
|
-
AbcSize:
|
7
|
-
Enabled: False
|
8
|
-
BlockNesting:
|
9
|
-
Max: 4
|
10
|
-
ClassLength:
|
11
|
-
Enabled: False
|
12
|
-
ClassVars:
|
13
|
-
Enabled: False
|
14
|
-
CyclomaticComplexity:
|
15
|
-
Enabled: False
|
16
|
-
Documentation:
|
17
|
-
Enabled: false
|
18
|
-
LineLength:
|
19
|
-
Max: 175
|
20
|
-
MethodLength:
|
21
|
-
Enabled: false
|
data/.ruby-version
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
2.3.0
|