class Thread::Delay

A delay is an object that incapsulates a block which is called upon value retrieval, and its result cached.

Public Class Methods

new(&block) click to toggle source

Create a delay with the passed block.

# File lib/thread/delay.rb, line 17
def initialize (&block)
        raise ArgumentError, 'no block given' unless block

        @mutex = Mutex.new
        @block = block
end

Public Instance Methods

!()
Alias for: value!
delivered?() click to toggle source

Check if the delay has been called.

# File lib/thread/delay.rb, line 39
def delivered?
        @mutex.synchronize {
                instance_variable_defined? :@value
        }
end
Also aliased as: realized?
exception() click to toggle source

Return the raised exception.

# File lib/thread/delay.rb, line 32
def exception
        @mutex.synchronize {
                @exception
        }
end
exception?() click to toggle source

Check if an exception has been raised.

# File lib/thread/delay.rb, line 25
def exception?
        @mutex.synchronize {
                instance_variable_defined? :@exception
        }
end
realized?()
Alias for: delivered?
value() click to toggle source

Get the value of the delay, if it's already been executed, return the cached result, otherwise execute the block and return the value.

In case the block raises an exception, it will be raised, the exception is cached and will be raised every time you access the value.

# File lib/thread/delay.rb, line 52
def value
        @mutex.synchronize {
                raise @exception if instance_variable_defined? :@exception

                return @value if instance_variable_defined? :@value

                begin
                        @value = @block.call
                rescue Exception => e
                        @exception = e

                        raise
                end
        }
end
Also aliased as: ~
value!() click to toggle source

Do the same as {#value}, but return nil in case of exception.

# File lib/thread/delay.rb, line 71
def value!
        begin
                value
        rescue Exception
                nil
        end
end
Also aliased as: !
~()
Alias for: value