I have a class:
class MyExecutor
@@dry_run = false
@@shared_instance = nil
def self.shared_instance
@@shared_instance ||= setup_new_instance()
end
# setup
def self.setup_new_instance (dry_run = false)
@@dry_run = dry_run
new
end
# execution
def self.exec_block(block)
if (@@dry_run)
# dry run, only output
else
# we-e-e-t, run all code here
end
end
end
and possible usage:
# here we want perform action
array.each { |elem|
elem.sub!(/one/,'two')
logger.debug "element now: #{elem}"
}
# here we do dry_run if needed
MyExecutor.exec_block {
array.each { |elem|
elem.sub!(/one/,'two')
logger.debug 'element now: #{elem}'
}
}
Dry run will only output logger.debug
. Could anybody explain how to implement such thing? Does Ruby have ready-solution?