Class: Future

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

Overview

A delayed-execution result, optimistcally evaluated in a new Thread.

Examples:

x = future { sleep 5; 1 + 2 }
# do stuff...
y = x * 2     # => 6.  blocks unless 5 seconds has passed.

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ Future

Parameters:

  • block (Proc)


17
18
19
20
21
22
# File 'lib/future.rb', line 17

def initialize(block)
  @promise = promise &block
  @thread = ::Thread.new do
    @promise.force
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



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

def method_missing(method, *args, &block)
  @promise.send(method, *args, &block) 
end

Instance Method Details

#forceAny

The value of the future’s evaluation. Blocks until result available.

Returns:

  • (Any)


27
28
29
30
# File 'lib/future.rb', line 27

def force
  @thread.join
  @promise
end