Skip to content

Thread-Local Variables #142

Answered by ioquatix
tleish asked this question in Q&A
May 6, 2021 · 5 comments · 16 replies
Discussion options

You must be logged in to vote

Here is a working proof of concept.. a total hack too haha:

require 'fiber'

module Inheritable
	def initialize(**options)
		super(**options)
		
		self.inherit_attributes_from(Fiber.current)
	end
	
	def self.prepended(klass)
		klass.extend(Singleton)
		klass.instance_variable_set(:@inheritable_attributes, Hash.new)
	end
	
	module Singleton
		def inheritable(key, default: nil)
			@inheritable_attributes[:"@#{key}"] = default
		end
		
		def inheritable_attributes
			@inheritable_attributes
		end
	end
	
	def inherit_attributes_from(fiber)
		self.class.inheritable_attributes.each do |name, default|
			value = fiber.instance_variable_get(name) || default
			self.instance_variable_set(name, value)

Replies: 5 comments 16 replies

Comment options

You must be logged in to vote
4 replies
@ioquatix
Comment options

@tleish
Comment options

@ioquatix
Comment options

@eregon
Comment options

Comment options

You must be logged in to vote
8 replies
@ioquatix
Comment options

@ioquatix
Comment options

@eregon
Comment options

@tleish
Comment options

@ioquatix
Comment options

Answer selected by tleish
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@ioquatix
Comment options

@ioquatix
Comment options

Comment options

You must be logged in to vote
2 replies
@ioquatix
Comment options

@nepthar
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants