rpn_ruby 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +49 -0
- data/Gemspec +3 -0
- data/LICENSE +2 -0
- data/README.md +29 -0
- data/Rakefile +11 -0
- data/lib/rpn_ruby.rb +49 -0
- data/lib/rpn_ruby/version.rb +3 -0
- data/rpn_ruby.gemspec +24 -0
- data/spec/lib/rpn_ruby/rpn_ruby_spec.rb +133 -0
- data/spec/spec_helper.rb +3 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cb499b6077e277300139ac9f0ac7d482b34a87a4
|
4
|
+
data.tar.gz: 5df5dd84ad8a5a189bd5f3e73926db60480d9151
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1dc3183bfc940039d623421fb781fd0f6cfb04fae560dfb3f4f0d4f6e7e53c177894282bf572bc95159bc259dd3fb119dd62d1113d3d5886fc09484ffcbf9502
|
7
|
+
data.tar.gz: df27924c1ac17ddda5b9ee8f1d3eade3f8c20706ce57efd96e85204330605ca66f35a602cc2feedc9d158552c23457c7195feb6285185fb9d4cd40842cc5bf07
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rpn_ruby (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.3.0)
|
10
|
+
diff-lcs (1.2.5)
|
11
|
+
parser (2.3.1.4)
|
12
|
+
ast (~> 2.2)
|
13
|
+
powerpack (0.1.1)
|
14
|
+
rainbow (2.1.0)
|
15
|
+
rake (11.3.0)
|
16
|
+
rspec (3.5.0)
|
17
|
+
rspec-core (~> 3.5.0)
|
18
|
+
rspec-expectations (~> 3.5.0)
|
19
|
+
rspec-mocks (~> 3.5.0)
|
20
|
+
rspec-core (3.5.4)
|
21
|
+
rspec-support (~> 3.5.0)
|
22
|
+
rspec-expectations (3.5.0)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.5.0)
|
25
|
+
rspec-mocks (3.5.0)
|
26
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
27
|
+
rspec-support (~> 3.5.0)
|
28
|
+
rspec-support (3.5.0)
|
29
|
+
rubocop (0.45.0)
|
30
|
+
parser (>= 2.3.1.1, < 3.0)
|
31
|
+
powerpack (~> 0.1)
|
32
|
+
rainbow (>= 1.99.1, < 3.0)
|
33
|
+
ruby-progressbar (~> 1.7)
|
34
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
35
|
+
ruby-progressbar (1.8.1)
|
36
|
+
unicode-display_width (1.1.1)
|
37
|
+
|
38
|
+
PLATFORMS
|
39
|
+
ruby
|
40
|
+
|
41
|
+
DEPENDENCIES
|
42
|
+
bundler (~> 1.7)
|
43
|
+
rake
|
44
|
+
rpn_ruby!
|
45
|
+
rspec
|
46
|
+
rubocop
|
47
|
+
|
48
|
+
BUNDLED WITH
|
49
|
+
1.11.2
|
data/Gemspec
ADDED
data/LICENSE
ADDED
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# rpn_ruby
|
2
|
+
|
3
|
+
A gem developed as part of a toy project to play with different ways of packaging and deploying applications
|
4
|
+
|
5
|
+
This repo is a submodule of the toy project ```rpn_sample_ruby```.
|
6
|
+
|
7
|
+
```rpn_ruby``` is a module containing the implementation of the RPN calculator logic; organized as a gem but not built and uploaded to rubygems.org.
|
8
|
+
|
9
|
+
## Local testing
|
10
|
+
|
11
|
+
To get started:
|
12
|
+
|
13
|
+
```shell
|
14
|
+
bundle install
|
15
|
+
```
|
16
|
+
|
17
|
+
To run the microtests:
|
18
|
+
|
19
|
+
```shell
|
20
|
+
rake
|
21
|
+
```
|
22
|
+
|
23
|
+
## Related projects
|
24
|
+
|
25
|
+
```rpn_service``` is a RESTful service that wraps module ```rpn_ruby```.
|
26
|
+
|
27
|
+
```rpn_ui``` is a Sinatra-based webapp that calls service ```rpn_service```.
|
28
|
+
|
29
|
+
|
data/Rakefile
ADDED
data/lib/rpn_ruby.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
class Numeric
|
2
|
+
def root rt
|
3
|
+
self**(1.0/rt)
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
module RPN
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@stack = []
|
11
|
+
end
|
12
|
+
|
13
|
+
def enter value
|
14
|
+
case value
|
15
|
+
when '='
|
16
|
+
return result
|
17
|
+
when '+'
|
18
|
+
@stack.push @stack.pop + @stack.pop
|
19
|
+
when '-'
|
20
|
+
subtrahend = @stack.pop
|
21
|
+
@stack.push @stack.pop - subtrahend
|
22
|
+
when '*'
|
23
|
+
@stack.push @stack.pop * @stack.pop
|
24
|
+
when '^'
|
25
|
+
exponent = @stack.pop
|
26
|
+
@stack.push @stack.pop ** exponent
|
27
|
+
when 'r'
|
28
|
+
rt = @stack.pop
|
29
|
+
@stack.push @stack.pop.root rt
|
30
|
+
when '/', 'd'
|
31
|
+
divisor = @stack.pop
|
32
|
+
@stack.push @stack.pop / divisor
|
33
|
+
when '%'
|
34
|
+
divisor = @stack.pop
|
35
|
+
@stack.push @stack.pop % divisor
|
36
|
+
else
|
37
|
+
@stack.push value.to_f
|
38
|
+
end
|
39
|
+
self
|
40
|
+
rescue Exception => @err
|
41
|
+
@stack.push 'invalid input'
|
42
|
+
return self
|
43
|
+
end
|
44
|
+
|
45
|
+
def result
|
46
|
+
@stack.first.to_s || ''
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
data/rpn_ruby.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rpn_ruby/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rpn_ruby"
|
8
|
+
spec.version = RPN::VERSION
|
9
|
+
spec.authors = ["Dave Nicolette"]
|
10
|
+
spec.email = ["davenicolette@gmail.com"]
|
11
|
+
spec.summary = %q{Reverse Polish Notation calculator}
|
12
|
+
spec.description = %q{Processes arithmetic expressions in postfix notation}
|
13
|
+
spec.homepage = "http://github.com/neopragma/rpn_ruby"
|
14
|
+
spec.license = "GPL-3.0"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = []
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rubocop", '~> 0'
|
24
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require_relative '../../../lib/rpn_ruby'
|
2
|
+
|
3
|
+
class RPN_Container
|
4
|
+
include RPN
|
5
|
+
end
|
6
|
+
|
7
|
+
describe RPN_Container do
|
8
|
+
|
9
|
+
before(:each) do
|
10
|
+
@calculator = RPN_Container.new
|
11
|
+
end
|
12
|
+
|
13
|
+
context "presenting the latest input:" do
|
14
|
+
|
15
|
+
describe "return the item at the top of the stack:" do
|
16
|
+
|
17
|
+
it "echoes the only number entered" do
|
18
|
+
expect(@calculator.enter('5.0').enter('='))
|
19
|
+
.to eq('5.0')
|
20
|
+
end
|
21
|
+
|
22
|
+
it "echoes the last number entered" do
|
23
|
+
expect(@calculator.enter('5.0').enter('3.54').enter('='))
|
24
|
+
.to eq('5.0')
|
25
|
+
end
|
26
|
+
|
27
|
+
it "returns an empty string when the stack is empty" do
|
28
|
+
expect(@calculator.result)
|
29
|
+
.to eq('')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "calculations:" do
|
35
|
+
|
36
|
+
describe "single operations:" do
|
37
|
+
|
38
|
+
it "addition" do
|
39
|
+
expect(@calculator.enter('5.25').enter('3.72').enter('+').enter('='))
|
40
|
+
.to eq('8.97')
|
41
|
+
end
|
42
|
+
|
43
|
+
it "subtraction" do
|
44
|
+
expect(@calculator.enter('66.5').enter('33.15').enter('-').enter('='))
|
45
|
+
.to eq('33.35')
|
46
|
+
end
|
47
|
+
|
48
|
+
it "multiplication" do
|
49
|
+
expect(@calculator.enter('2.2').enter('0.4').enter('*').enter('='))
|
50
|
+
.to eq('0.8800000000000001')
|
51
|
+
end
|
52
|
+
|
53
|
+
it "division" do
|
54
|
+
expect(@calculator.enter('10.88').enter('2').enter('/').enter('='))
|
55
|
+
.to eq('5.44')
|
56
|
+
end
|
57
|
+
|
58
|
+
it "division using d instead of /" do
|
59
|
+
expect(@calculator.enter('10.88').enter('2').enter('d').enter('='))
|
60
|
+
.to eq('5.44')
|
61
|
+
end
|
62
|
+
|
63
|
+
it "modulo" do
|
64
|
+
expect(@calculator.enter('56').enter('5').enter('%').enter('='))
|
65
|
+
.to eq('1.0')
|
66
|
+
end
|
67
|
+
|
68
|
+
it "exponentiation" do
|
69
|
+
expect(@calculator.enter('2').enter('5').enter('^').enter('='))
|
70
|
+
.to eq('32.0')
|
71
|
+
end
|
72
|
+
|
73
|
+
it "roots" do
|
74
|
+
expect(@calculator.enter('81').enter('4').enter('r').enter('='))
|
75
|
+
.to eq('3.0')
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "multiple operations:" do
|
80
|
+
|
81
|
+
it "handles the equivalent of (6(4+5))" do
|
82
|
+
expect(@calculator
|
83
|
+
.enter('6')
|
84
|
+
.enter('4')
|
85
|
+
.enter('5')
|
86
|
+
.enter('+')
|
87
|
+
.enter('*')
|
88
|
+
.enter('='))
|
89
|
+
.to eq('54.0')
|
90
|
+
end
|
91
|
+
|
92
|
+
it "handles the equivalent of (6(4+5))-(25/(2+3))" do
|
93
|
+
expect(@calculator
|
94
|
+
.enter('6')
|
95
|
+
.enter('4')
|
96
|
+
.enter('5')
|
97
|
+
.enter('+')
|
98
|
+
.enter('*')
|
99
|
+
.enter('25')
|
100
|
+
.enter('2')
|
101
|
+
.enter('3')
|
102
|
+
.enter('+')
|
103
|
+
.enter('/')
|
104
|
+
.enter('-')
|
105
|
+
.enter('='))
|
106
|
+
.to eq('49.0')
|
107
|
+
end
|
108
|
+
|
109
|
+
it "calculates the area of a circle" do
|
110
|
+
expect(@calculator
|
111
|
+
.enter(Math::PI)
|
112
|
+
.enter('3')
|
113
|
+
.enter('2')
|
114
|
+
.enter('^')
|
115
|
+
.enter('*')
|
116
|
+
.enter('=').to_f)
|
117
|
+
.to be_within(0.005).of(28.27)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context "handling incorrect input:" do
|
123
|
+
|
124
|
+
describe "too few values for an operation:" do
|
125
|
+
|
126
|
+
it "does something reasonable" do
|
127
|
+
expect(@calculator.enter('5.0').enter('*').enter('='))
|
128
|
+
.to eq('invalid input')
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rpn_ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dave Nicolette
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rubocop
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Processes arithmetic expressions in postfix notation
|
56
|
+
email:
|
57
|
+
- davenicolette@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- Gemfile.lock
|
65
|
+
- Gemspec
|
66
|
+
- LICENSE
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/rpn_ruby.rb
|
70
|
+
- lib/rpn_ruby/version.rb
|
71
|
+
- rpn_ruby.gemspec
|
72
|
+
- spec/lib/rpn_ruby/rpn_ruby_spec.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
homepage: http://github.com/neopragma/rpn_ruby
|
75
|
+
licenses:
|
76
|
+
- GPL-3.0
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.5.1
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Reverse Polish Notation calculator
|
98
|
+
test_files:
|
99
|
+
- spec/lib/rpn_ruby/rpn_ruby_spec.rb
|
100
|
+
- spec/spec_helper.rb
|