class Hammer::Internal::DynamicVariable
Constants
- SYMBOL_PREFIX
Public Class Methods
new(default=nil, name=nil, &block)
click to toggle source
# File lib/hammer/internal.rb, line 13 def initialize(default=nil, name=nil, &block) # This can take either a default value or a block. If a # default value is given, all threads' dynvars are initialized # to that object. If a block is given, the block is lazilly # called on each thread to generate the initial value. If # both a block and a default value are passed, the block is # called with the literal value. @default = default @block = block || Proc.new{|x| x} @@current_symbol += 1 @sym = (SYMBOL_PREFIX + @@current_symbol.to_s).to_sym end
Public Instance Methods
value()
click to toggle source
# File lib/hammer/internal.rb, line 26 def value if Thread.current.key? @sym return Thread.current[@sym] else return Thread.current[@sym] = @block.call(@default) end end
value=(new_value)
click to toggle source
# File lib/hammer/internal.rb, line 34 def value=(new_value) Thread.current[@sym] = new_value end
with(new_value, &block)
click to toggle source
# File lib/hammer/internal.rb, line 38 def with(new_value, &block) old_value = value begin self.value = new_value return block.call ensure self.value = old_value end end