Skip to content

Instantly share code, notes, and snippets.

@banyan
Created November 28, 2012 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banyan/4163069 to your computer and use it in GitHub Desktop.
Save banyan/4163069 to your computer and use it in GitHub Desktop.
class Gear
attr_reader :chainring, :cog, :wheel
def initialize(chainring, cog, wheel = nil)
@chainring = chainring
@cog = cog
@wheel = wheel
end
def ratio
chainring / cog.to_f
end
def gear_inches
ratio * wheel.diameter
end
end
class Wheel
attr_reader :rim, :tire
def initialize(rim, tire)
@rim = rim
@tire = tire
end
def diameter
rim + (tire * 2)
end
def circumference
diameter * Math::PI
end
end
wheel = Wheel.new(12, 3)
p wheel.circumference
p Gear.new(52, 11, wheel).gear_inches
p Gear.new(52, 11).gear_inches
# 56.548667764616276
# 85.0909090909091
# /Users/banyan/lang/ruby/POOD/chapter2.rb:15:in `gear_inches': undefined method `diameter' for nil:NilClass (NoMethodError)
# from /Users/banyan/lang/ruby/POOD/chapter2.rb:40:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment