arithmetic 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YTFkMzQwNDk4YWVkZTk1NWY2NTA4M2Y3MTUyODc5ZTg5MWZkOTE2MA==
5
+ data.tar.gz: !binary |-
6
+ Yzg1NTkyYmNiOTUzYzQ4ZTQyYTg0ODZlZTI5NzEwNGViZWRhODA4ZA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MDY3YTA4ZmE4Yzc1MzA5YjQxZDI5N2ZiYWNhMmNmYTZhZWUxOTI0NDgwYmE2
10
+ NjhmYjVmYWJmNWJkYjQ5Mjk0NGU5MzY1NWJkOTNjNWM1ODNkNDZmZDI0ODgy
11
+ OGRhMjFmNjY2YjNkZGM0NTQzZmY2ZGJiM2U3ODcyYjg1MTg3Y2M=
12
+ data.tar.gz: !binary |-
13
+ YTQ4M2FmYTJhYTkxZWU2ODYxOTI3ZjIxNzhjZjAwNjEyNGQwY2NiMDAzNDE0
14
+ OGRiNDdkOGUxZDg3NzQwNmU3OGU5NTNiYjAxOThkOTE5MmMzNzVhMjk0ZDVl
15
+ MGEwOTEzNzljM2Y2OTE1Y2EwOTlmYTk1MDgzMjFlN2VhNjhjMWI=
@@ -1,12 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- arithmetic (0.0.1)
4
+ arithmetic (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  diff-lcs (1.1.3)
10
+ rake (10.4.2)
10
11
  rspec (2.11.0)
11
12
  rspec-core (~> 2.11.0)
12
13
  rspec-expectations (~> 2.11.0)
@@ -21,4 +22,5 @@ PLATFORMS
21
22
 
22
23
  DEPENDENCIES
23
24
  arithmetic!
25
+ rake
24
26
  rspec
@@ -17,4 +17,5 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
  gem.add_development_dependency('rspec')
20
+ gem.add_development_dependency('rake')
20
21
  end
@@ -9,4 +9,8 @@ module Arithmetic
9
9
  def self.parse(expression)
10
10
  Expression.new(Parser.new(expression).parse)
11
11
  end
12
+
13
+ def self.is_operand?(token)
14
+ !token.is_a?(Arithmetic::Operator) && token != "(" && token != ")"
15
+ end
12
16
  end
@@ -8,8 +8,14 @@ module Arithmetic
8
8
  @result ||= @parsed_expression.eval
9
9
  end
10
10
 
11
- def to_s
12
- @string ||= @parsed_expression.to_s
11
+ def to_s(visitor=identity_function)
12
+ @string ||= @parsed_expression.to_s(visitor)
13
+ end
14
+
15
+ private
16
+
17
+ def identity_function
18
+ ->(node) { node }
13
19
  end
14
20
  end
15
21
  end
@@ -6,8 +6,8 @@ module Arithmetic
6
6
  @operand = operand
7
7
  end
8
8
 
9
- def to_s(na=nil)
10
- @operand
9
+ def to_s(visitor, na=nil)
10
+ visitor.call(@operand)
11
11
  end
12
12
 
13
13
  def eval
@@ -23,14 +23,14 @@ module Arithmetic
23
23
  @operands = operands
24
24
  end
25
25
 
26
- def to_s(top=true)
27
- strs = @operands.map {|o| o.to_s(false) }
26
+ def to_s(visitor, top=true)
27
+ strs = @operands.map {|o| o.to_s(visitor, false) }
28
28
 
29
29
  if @operator.arity == 1
30
- "#{@operator}#{strs.first}"
30
+ "#{visitor.call(@operator)}#{strs.first}"
31
31
  else
32
- result = [strs.first, @operator, *strs[1..-1]].join(" ")
33
- result = "(" + result + ")" unless top
32
+ result = [strs.first, visitor.call(@operator), *strs[1..-1]].join(" ")
33
+ result = visitor.call("(") + result + visitor.call(")") unless top
34
34
  result
35
35
  end
36
36
  end
@@ -1,3 +1,3 @@
1
1
  module Arithmetic
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -57,6 +57,10 @@ describe Arithmetic do
57
57
  test_to_s(" -1+\n 2* \t3").should == '-1 + (2 * 3)'
58
58
  end
59
59
 
60
+ it "formats the expression using a custom visitor" do
61
+ test_to_s("-1 + 2 * 3", ->(n) { "x#{n}x" }).should == 'x-xx1x x+x x(xx2x x*x x3xx)x'
62
+ end
63
+
60
64
  it "handles ridiculous precision" do
61
65
  test_eval("1.111111111111111111111111111111111111111111 + 2").should == BigDecimal.new('3.111111111111111111111111111111111111111111')
62
66
  end
@@ -100,8 +104,8 @@ describe Arithmetic do
100
104
  test_init(exp).eval
101
105
  end
102
106
 
103
- def test_to_s(exp)
104
- test_init(exp).to_s
107
+ def test_to_s(exp, visitor=nil)
108
+ test_init(exp).to_s(*visitor)
105
109
  end
106
110
 
107
111
  def test_init(exp)
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arithmetic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
5
- prerelease:
4
+ version: 0.1.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Sean Kirby
@@ -10,12 +9,11 @@ authors:
10
9
  autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2012-10-26 00:00:00.000000000 Z
12
+ date: 2015-06-17 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: rspec
17
16
  requirement: !ruby/object:Gem::Requirement
18
- none: false
19
17
  requirements:
20
18
  - - ! '>='
21
19
  - !ruby/object:Gem::Version
@@ -23,7 +21,20 @@ dependencies:
23
21
  type: :development
24
22
  prerelease: false
25
23
  version_requirements: !ruby/object:Gem::Requirement
26
- none: false
24
+ requirements:
25
+ - - ! '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
27
38
  requirements:
28
39
  - - ! '>='
29
40
  - !ruby/object:Gem::Version
@@ -53,27 +64,26 @@ files:
53
64
  - spec/spec_helper.rb
54
65
  homepage: https://github.com/nulogy/arithmetic
55
66
  licenses: []
67
+ metadata: {}
56
68
  post_install_message:
57
69
  rdoc_options: []
58
70
  require_paths:
59
71
  - lib
60
72
  required_ruby_version: !ruby/object:Gem::Requirement
61
- none: false
62
73
  requirements:
63
74
  - - ! '>='
64
75
  - !ruby/object:Gem::Version
65
76
  version: '0'
66
77
  required_rubygems_version: !ruby/object:Gem::Requirement
67
- none: false
68
78
  requirements:
69
79
  - - ! '>='
70
80
  - !ruby/object:Gem::Version
71
81
  version: '0'
72
82
  requirements: []
73
83
  rubyforge_project:
74
- rubygems_version: 1.8.24
84
+ rubygems_version: 2.4.3
75
85
  signing_key:
76
- specification_version: 3
86
+ specification_version: 4
77
87
  summary: Simple arithmetic calculator for Ruby
78
88
  test_files:
79
89
  - spec/arithmetic_spec.rb