Slide 6
Slide 6 text
Solutions
class Gear
attr_reader :chainring, :cog, :rim, :tire
def initialize(chainring:, cog:, rim: nil, tire: nil)
@chainring = chainring
@cog = cog
@rim = rim
@tire = tire
end
def ratio
chainring / cog.to_f
end
def gear_inches
ratio * (rim + (tire * 2))
end
end
class Gear
attr_reader :chainring, :cog, :wheel
def initialize(chainring:, cog:, rim: nil, tire: nil)
@chainring = chainring
@cog = cog
@wheel = Wheel.new(rim, tire)
end
def ratio
chainring / cog.to_f
end
def gear_inches
ratio * wheel.diameter
end
Wheel = Struct.new(:rim, :tire) do
def diameter
rim + (tire * 2)
end
def circumference
diameter * Math::PI
end
end
end
Isolate problematic code
using Struct
Always access instance
variables via getters.
Makes it easy to add logic
to getters without changing
everywhere instance
variable is used.