Class: FutureProof::Future

Inherits:
Object
  • Object
show all
Defined in:
lib/future_proof/future.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Future

Initializes new Future.

Examples:

Initialize Future

FutureProof::Future.new { |a, b| a + b }


11
12
13
# File 'lib/future_proof/future.rb', line 11

def initialize(&block)
  @block = block
end

Instance Method Details

#call(*args) ⇒ Object

Executes Future in a thread with given params.

Parameters:

  • *args (Array<Object>)

    proc arguments



21
22
23
# File 'lib/future_proof/future.rb', line 21

def call(*args)
  thread(*args)
end

#complete?true, false

Return true if Future is done working.

Returns:

  • (true, false)

    true if Future is done working.



43
44
45
# File 'lib/future_proof/future.rb', line 43

def complete?
  !thread.alive?
end

#value(*args) ⇒ Object

Note:

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

Return a result of a Future.

Parameters:

  • *args (Array<Object>)

    proc arguments if they were not suplied before.



33
34
35
# File 'lib/future_proof/future.rb', line 33

def value(*args)
  thread(*args).value
end