如何傳遞processor_pool給模塊內的方法?
class Dummy
def initialize
processor_pool = Concurrent::FixedThreadPool.new(10)
@threadpool = Module.new do
extend Concurrent::Promises::FactoryMethods
def self.default_executor
return processor_pool # this cannot find the processor_pool variable
end
end
end
end
即使我將其設為實體變數,我也會收到相同的錯誤 @processor_pool
uj5u.com熱心網友回復:
像這樣的東西(為了示例,我稍微簡化了你的類以擺脫依賴關系,但它的結構是相同的):
class Dummy
attr_reader :threadpool
def initialize
processor_pool = "It works"
@threadpool = Module.new do
define_method :default_executor do
return processor_pool # this cannot find the processor_pool variable
end
module_function :default_executor
end
end
end
Dummy.new.threadpool.default_executor # => "It works"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/418354.html
標籤:
下一篇:我怎樣才能完成填充陣列?
