dentaku 0.2.6 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -36,8 +36,8 @@ module Dentaku
36
36
  P_COMBINE = [T_LOGICAL, T_COMBINATOR, T_LOGICAL]
37
37
 
38
38
  P_IF = [T_IF, T_OPEN, T_NON_GROUP, T_COMMA, T_NON_GROUP, T_COMMA, T_NON_GROUP, T_CLOSE]
39
- P_ROUND_ONE = [T_ROUND, T_OPEN, T_NUMERIC, T_CLOSE]
40
- P_ROUND_TWO = [T_ROUND, T_OPEN, T_NUMERIC, T_COMMA, T_NUMERIC, T_CLOSE]
39
+ P_ROUND_ONE = [T_ROUND, T_OPEN, T_NON_GROUP_STAR, T_CLOSE]
40
+ P_ROUND_TWO = [T_ROUND, T_OPEN, T_NON_GROUP_STAR, T_COMMA, T_NUMERIC, T_CLOSE]
41
41
  P_NOT = [T_NOT, T_OPEN, T_NON_GROUP_STAR, T_CLOSE]
42
42
 
43
43
  RULES = [
@@ -152,18 +152,18 @@ module Dentaku
152
152
  end
153
153
 
154
154
  def round(*args)
155
- function = args.shift
156
- open = args.shift
157
- input = args.shift.value
158
- places = 0
159
-
160
- if args.length > 1
161
- comma = args.shift
162
- places = args.shift.value
155
+ _, _, *tokens, _ = args
156
+
157
+ input_tokens = tokens.take_while { |a| a.category != :grouping }
158
+ input_value = evaluate_token_stream(input_tokens).value
159
+ places = 0
160
+
161
+ if places_token = tokens.drop_while { |a| a.category != :grouping }.last
162
+ places = places_token.value
163
163
  end
164
164
 
165
165
  begin
166
- value = input.round(places)
166
+ value = input_value.round(places)
167
167
  rescue ArgumentError
168
168
  value = (input * 10 ** places).round / (10 ** places).to_f
169
169
  end
@@ -1,3 +1,3 @@
1
1
  module Dentaku
2
- VERSION = "0.2.6"
2
+ VERSION = "0.2.7"
3
3
  end
@@ -55,6 +55,12 @@ describe Dentaku::Evaluator do
55
55
  end
56
56
  end
57
57
 
58
+ describe 'functions' do
59
+ it 'should evaluate function' do
60
+ evaluator.evaluate(token_stream(:round, :open, 5, :divide, 3.0, :close)).should eq 2
61
+ end
62
+ end
63
+
58
64
  describe 'logic' do
59
65
  it 'should evaluate conditional' do
60
66
  evaluator.evaluate(token_stream(5, :gt, 1)).should be_true
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dentaku
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-20 00:00:00.000000000 Z
12
+ date: 2012-09-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70134892086100 !ruby/object:Gem::Requirement
16
+ requirement: &70128193331500 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70134892086100
24
+ version_requirements: *70128193331500
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70134892085420 !ruby/object:Gem::Requirement
27
+ requirement: &70128193330880 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70134892085420
35
+ version_requirements: *70128193330880
36
36
  description: ! ' Dentaku is a parser and evaluator for mathematical formulas
37
37
 
38
38
  '