Skip to content

Instantly share code, notes, and snippets.

@CheezItMan
Created March 2, 2017 16:46
Show Gist options
  • Save CheezItMan/a50dd607c158808995e62de86057db1a to your computer and use it in GitHub Desktop.
Save CheezItMan/a50dd607c158808995e62de86057db1a to your computer and use it in GitHub Desktop.
# sample_code.rb from POOD Ch 3
# Starter 1 - Heaviliy dependent Gear class
class Gear
attr_reader :chainring, :cog, :rim, :tire
def initialize(chainring, cog, rim, tire)
@chainring = chainring
@cog = cog
@rim = rim
@tire = tire
end
def gear_inches
ratio * Wheel.new(rim, tire).diameter
end
def ratio
chainring / cog.to_f
end
end
class Wheel
attr_reader :rim, :tire
def initialize(rim, tire)
@rim = rim
@tire = tire
end
def diameter
rim + (tire * 2)
end
end
puts "The gear is #{Gear.new(52, 11, 26, 1.5).gear_inches} inches "
# Sample 2 Less dependent
# Starter 1 - Heaviliy dependent Gear class
# Using dependency injection
class Gear
attr_reader :chainring, :cog, :rim, :tire
def initialize(chainring, cog, wheel)
@chainring = chainring
@cog = cog
@wheel = wheel
end
def gear_inches
ratio * wheel.diameter
end
def ratio
chainring / cog.to_f
end
end
# Example 3 Two ways of isolating dependency
# class Gear
# attr_reader :chainring, :cog, :rim, :tire
#
# def initialize(chainring, cog, rim, tire)
# @chainring = chainring
# @cog = cog
#
# # Wheel is isolated to the initialize method
# @wheel = Wheel.new(rim, tire)
# end
#
# def gear_inches
# ratio * wheel.diameter
# end
#
# def ratio
# chainring / cog.to_f
# end
# end
#
# class Gear
# attr_reader :chainring, :cog, :rim, :tire
#
# def initialize(chainring, cog, rim, tire)
# @chainring = chainring
# @cog = cog
# @rim = rim
# @tire = tire
# end
#
# # Wheel is isolated to a method
# def wheel
# @wheel ||= Wheel.new(@rim, @tire)
# end
#
# def diameter
# wheel.diameter
# end
#
# def gear_inches
# ratio * diameter
# end
#
# def ratio
# chainring / cog.to_f
# end
# end
# class Gear
# attr_reader :chainring, :cog, :rim, :tire
#
# # Removing order dependency by using a hash parameter
# def initialize(args)
# @chainring = args[:chainring]
# @cog = args[:cog]
# @rim = args[:rim]
# @tire = args[:tire]
# end
#
# # Wheel is isolated to a method
# def wheel
# @wheel ||= Wheel.new(@rim, @tire)
# end
#
# def diameter
# wheel.diameter
# end
#
# def gear_inches
# ratio * diameter
# end
#
# def ratio
# chainring / cog.to_f
# end
# end
# Example using || for default arguments
#
# class Gear
# attr_reader :chainring, :cog, :rim, :tire
#
# def initialize(args)
# @chainring = args[:chainring] || 40
# @cog = args[:cog] || 11
# # Could also use args.fetch
# # @cog = args.fetch(:cog, 22)
# @rim = args[:rim]
# @tire = args[:tire]
# end
#
# # def initialize(args)
# # args = defaults.merge(args)
# # @chainring = args[:chainring]
# # @cog = args[:cog]
# # @rim = args[:rim]
# # @tire = args[:tire]
# # end
#
# # Use when default arguments are more complicated than this!
# # def defaults
# # { :chainring => 40, :cog => 18}
# # end
#
#
# # Wheel is isolated to a method
# def wheel
# @wheel ||= Wheel.new(@rim, @tire)
# end
#
# def diameter
# wheel.diameter
# end
#
# def gear_inches
# ratio * diameter
# end
#
# def ratio
# chainring / cog.to_f
# end
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment