Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 4 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
Showing with 79 additions and 27 deletions.
  1. +2 −0 Gemfile
  2. +4 −0 Gemfile.lock
  3. +17 −16 lib/calculator.rb
  4. +27 −0 lib/graph.rb
  5. +8 −0 misc.rb
  6. +21 −11 spec/calculator_spec.rb
View
2 Gemfile
@@ -1,5 +1,7 @@
source "https://rubygems.org"
+gem "gruff"
+
group :test, :development do
gem "rspec", "~> 2.14.1"
end
View
4 Gemfile.lock
@@ -2,6 +2,9 @@ GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.2.4)
+ gruff (0.5.1)
+ rmagick
+ rmagick (2.13.2)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
@@ -15,4 +18,5 @@ PLATFORMS
ruby
DEPENDENCIES
+ gruff
rspec (~> 2.14.1)
View
33 lib/calculator.rb
@@ -2,14 +2,14 @@ class GearRatio
class Calculator
- attr_accessor :chainring_sizes
- attr_accessor :cog_sizes
+ attr :chainring_sizes
+ attr :cog_sizes
attr_accessor :crank_length
attr_accessor :wheel_circumference
def initialize(chainring_sizes: nil, cog_sizes: nil, crank_length: nil, wheel_circumference: nil)
- @chainring_sizes = initialize_chainring_sizes(chainring_sizes)
- @cog_sizes = initialize_cog_sizes(cog_sizes)
+ self.chainring_sizes = chainring_sizes
+ self.cog_sizes = cog_sizes
@crank_length = crank_length
@wheel_circumference = wheel_circumference
end
@@ -35,6 +35,19 @@ def kph_speeds_at_cadence(cadence)
ret
end
+ # accepts integer, string, array
+ # sets a sorted array of integers
+ def chainring_sizes=(sizes)
+ @chainring_sizes = sizes.to_s.scan(/\d+/).map{ |c| c.to_i }.sort.reverse
+ end
+
+ # accepts integer, string, array
+ # sets a sorted array of integers
+ def cog_sizes=(sizes)
+ @cog_sizes = sizes.to_s.scan(/\d+/).map{ |c| c.to_i }.sort
+ end
+
+
private
@@ -43,18 +56,6 @@ def gain_ratio(chainring_size, cog_size)
(ratio * 100).to_i/100.0
end
- # accepts integer, string, array
- # returns a sorted array of integers
- def initialize_chainring_sizes(chainring_sizes)
- chainring_sizes.to_s.scan(/\d+/).map{ |c| c.to_i }.sort.reverse
- end
-
- # accepts integer, string, array
- # returns a sorted array of integers
- def initialize_cog_sizes(cog_sizes)
- cog_sizes.to_s.scan(/\d+/).map{ |c| c.to_i }.sort
- end
-
def wheel_diameter
wheel_circumference / Math::PI
end
View
27 lib/graph.rb
@@ -0,0 +1,27 @@
+class GearRatio
+
+ class Graph
+ require 'gruff'
+
+ def initialize(calculator)
+ @calculator = calculator
+ end
+
+ def gain_ratio_line_chart
+ g = Gruff::Line.new
+ g.title = 'Gain Ratios'
+ hsh = {}
+ @calculator.cog_sizes.each_with_index do |c, idx|
+ hsh[idx] = c.to_s
+ end
+ g.labels = hsh
+
+ @calculator.chainring_sizes.each_with_index do |chainring_size, idx|
+ g.data chainring_size.to_s, @calculator.gain_ratios[idx].reverse
+ end
+ g.write('gain_ratios.png')
+ end
+
+ end
+
+end
View
8 misc.rb
@@ -0,0 +1,8 @@
+load "lib/calculator.rb"
+load "lib/graph.rb"
+
+calc = GearRatio::Calculator.new chainring_sizes: "44 33", cog_sizes: "11 13 15 17 21 24 28 32 36 40", crank_length: 200, wheel_circumference: 2158
+calc.gain_ratios
+graph = GearRatio::Graph.new(calc)
+graph.gain_ratio_line_chart
+`mv gain_ratios.png ~/Desktop/`
View
32 spec/calculator_spec.rb
@@ -18,48 +18,58 @@
calc.wheel_circumference.should eql 2168
end
- describe "#initialize_chainring_sizes" do
+ describe "#chainring_sizes=" do
it "reverse-sorts the values" do
- @calc.send(:initialize_chainring_sizes, [30, 39, 52]).should eql [52, 39, 30]
+ @calc.chainring_sizes = [30, 39, 52]
+ @calc.chainring_sizes.should eql [52, 39, 30]
end
it "works with a string of values" do
- @calc.send(:initialize_chainring_sizes, "30 39 52").should eql [52, 39, 30]
+ @calc.chainring_sizes = "30 39 52"
+ @calc.chainring_sizes.should eql [52, 39, 30]
end
it "works with a single value" do
- @calc.send(:initialize_chainring_sizes, "40").should eql [40]
+ @calc.chainring_sizes = "40"
+ @calc.chainring_sizes.should eql [40]
end
it "works with a single value inside an array" do
- @calc.send(:initialize_chainring_sizes, [40]).should eql [40]
+ @calc.chainring_sizes = [40]
+ @calc.chainring_sizes.should eql [40]
end
it "works with a single string inside an array" do
- @calc.send(:initialize_chainring_sizes, ["40"]).should eql [40]
+ @calc.chainring_sizes = ["40"]
+ @calc.chainring_sizes.should eql [40]
end
end
describe "#initialize_cog_sizes" do
it "sorts the values" do
- @calc.send(:initialize_cog_sizes, [15, 14, 13, 12, 11]).should eql [11, 12, 13, 14, 15]
+ @calc.cog_sizes = [15, 14, 13, 12, 11]
+ @calc.cog_sizes.should eql [11, 12, 13, 14, 15]
end
it "works with a string of values" do
- @calc.send(:initialize_cog_sizes, "15, 14, 13, 12, 11").should eql [11, 12, 13, 14, 15]
+ @calc.cog_sizes = "15, 14, 13, 12, 11"
+ @calc.cog_sizes.should eql [11, 12, 13, 14, 15]
end
it "works with a single value" do
- @calc.send(:initialize_cog_sizes, "18").should eql [18]
+ @calc.cog_sizes = "18"
+ @calc.cog_sizes.should eql [18]
end
it "works with a single value inside an array" do
- @calc.send(:initialize_cog_sizes, [18]).should eql [18]
+ @calc.cog_sizes = [18]
+ @calc.cog_sizes.should eql [18]
end
it "works with a single string inside an array" do
- @calc.send(:initialize_cog_sizes, ["18"]).should eql [18]
+ @calc.cog_sizes = ["18"]
+ @calc.cog_sizes.should eql [18]
end
end

No commit comments for this range

Something went wrong with that request. Please try again.