Skip to content

Instantly share code, notes, and snippets.

@elct9620
Created November 4, 2021 09:15
Show Gist options
  • Save elct9620/5958de0fdb31b7961670fb05cb90001b to your computer and use it in GitHub Desktop.
Save elct9620/5958de0fdb31b7961670fb05cb90001b to your computer and use it in GitHub Desktop.
Ruby Block-based Refactor
class BadObject
def initialize
@internal = 'Coupling'
end
def perform
puts 'Start'
100.times do |i|
puts "Long Code ... #{i} #{@internal}"
end
puts 'End'
end
end
puts '=== Bad Object ==='
BadObject.new.perform
class RefactorObject
def initialize(&block)
@i_dont_care = block
end
def perform
puts 'Start'
@i_dont_care.call
puts 'End'
end
end
class RefactorBadObject
def initialize
@internal = 'Coupling'
end
def perform
RefactorObject.new do
100.times do |i|
puts "Long Code ... #{i} #{@internal}"
end
end.perform
end
end
puts '=== Refactor Bad Object ==='
RefactorBadObject.new.perform
class DecouplingAction
def initialize(idx, ref)
@idx = idx
@ref = ref
end
def perform
puts "Long Code ... #{@idx} #{@ref}"
end
end
class RefactorDecouplingObject
def initialize(ref)
@ref = ref
end
def perform
puts 'Start'
100.times { |i| DecouplingAction.new(i, @ref).perform }
puts 'End'
end
end
class RefactorDecouplingBadObject
def initialize
@internal = 'Coupling'
end
def perform
RefactorDecouplingObject.new(@internal).perform
end
end
puts '=== Refactor Bad Object ==='
RefactorDecouplingBadObject.new.perform
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment