class FutureProof::Future

Class Futures can be used to create Procs that should be executed inside a Thread.

Public Class Methods

new(&block) click to toggle source

Initializes new Future.

@example Initialize Future

FutureProof::Future.new { |a, b| a + b }
# File lib/future_proof/future.rb, line 11
def initialize(&block)
  @block = block
end

Public Instance Methods

call(*args) click to toggle source

Executes Future in a thread with given params.

@param [Array<Object>] *args proc arguments

@exmaple call Future

future.call(2, 2)
# File lib/future_proof/future.rb, line 21
def call(*args)
  thread(*args)
end
complete?() click to toggle source

Return true if Future is done working.

@exmaple Get state of Future

future.complete?

@return [true, false] true if Future is done working.

# File lib/future_proof/future.rb, line 43
def complete?
  !thread.alive?
end
value(*args) click to toggle source

Return a result of a Future.

@param [Array<Object>] *args proc arguments if they were not suplied before.

@note It requires list of arguments if Future was not called before.

@exmaple Get value of Future

future.value
# File lib/future_proof/future.rb, line 33
def value(*args)
  thread(*args).value
end

Private Instance Methods

thread(*args) click to toggle source

Thread that Future is wrapped around.

# File lib/future_proof/future.rb, line 50
def thread(*args)
  @thread ||= Thread.new { @block.call *args }
end