Created
July 20, 2015 20:09
-
-
Save allolex/fe28846fb7ab41b3d4b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require_relative "wheel" | |
| require_relative "gear" | |
| class Bicycle | |
| attr_reader :wheel, :gear | |
| def initialize(wheel = Wheel.new(:racing), gear = Gear.new(:high)) | |
| @wheel = wheel | |
| @gear = gear | |
| end | |
| def speed | |
| wheel.diameter * gear.ratio | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Gear | |
| attr_reader :speed | |
| RATIOS = { | |
| high: 10, | |
| mid: 6, | |
| low: 2 | |
| } | |
| def initialize(speed = :mid) | |
| @speed = speed | |
| end | |
| def ratio | |
| RATIOS[speed.to_sym] | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require_relative "bicycle" | |
| def runtest(expression) | |
| puts expression ? "Passed" : "Failed" | |
| # puts if expression | |
| # "Passed" | |
| # else | |
| # "Failed" | |
| # end | |
| end | |
| def test_default | |
| puts "A new Bicycle has a default wheel type of 'racing'" | |
| puts "and a default gear of 'high'" | |
| b = Bicycle.new | |
| runtest(b.speed == 300) | |
| end | |
| def test_childrens | |
| w = Wheel.new(:childrens) | |
| puts "A child's bicycle in low gear has a speed of 48." | |
| g = Gear.new(:low) | |
| b = Bicycle.new(w, g) | |
| runtest(b.speed == 48) | |
| puts "A child's bicycle in mid gear has a speed of 144." | |
| g = Gear.new(:mid) | |
| b = Bicycle.new(w, g) | |
| runtest(b.speed == 144) | |
| puts "A child's bicycle in high gear has a speed of 240." | |
| g = Gear.new(:high) | |
| b = Bicycle.new(w, g) | |
| runtest(b.speed == 240) | |
| end | |
| test_default | |
| test_childrens |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Wheel | |
| attr_reader :type | |
| def initialize(type = :touring) | |
| @type = type | |
| end | |
| def diameter | |
| lookup[type.to_sym] | |
| end | |
| private | |
| def lookup | |
| { | |
| racing: 30, | |
| mountain: 26, | |
| touring: 28, | |
| childrens: 24 | |
| } | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment