Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

316
Views
Ruby: How to reference a variable defined outside of a module

How do I pass processor_pool to the method inside the module?

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

I get the same error even if I make it an instance variable like @processor_pool

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Something like this (I simplified your class a bit to get rid of dependencies for the sake of example, but its structure is the same):

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"
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!