如果我将块存储在控制器的类变量中,然后从实例中调用它,是否会导致线程范围出现任何问题?传递给块的参数对于每个实例都是本地的。
这是代码示例:
class BlockExecutor
def initialize(&block)
self.instance_eval &block
end
def sub_block(&block)
@block = block
end
def call(object)
@block.call(object)
end
end
module ClassDSL
extend ActiveSupport::Concern
module ClassMethods
def define(&block)
@executor = BlockExecutor.new &block
end
def execute_block(object)
@executor.call(object)
end
end
end
class Controller
include ClassDSL
define do
sub_block do |object|
# mutate object
end
end
def run
new_object = self.class.execute_block(object)
end
end