Class: Future

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

Overview

A delayed-execution result, optimistically 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

- (Future) initialize { ... }

Creates a new future.

Yields:

  • [] The block to evaluate optimistically.

See Also:



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

def initialize(&block)
  @promise = ::Promise.new(&block)
  @thread  = ::Thread.new { @promise.__force__ }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method, *args, &block) (private)



45
46
47
# File 'lib/future.rb', line 45

def method_missing(method, *args, &block)
  __force__.__send__(method, *args, &block)
end

Instance Method Details

- (Object) __force__ Also known as: force

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

Returns:

  • (Object)


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

def __force__
  @thread.join if @thread
  @promise
end

- (Boolean) respond_to?(method)

Does this future support the given method?

Parameters:

  • (Symbol)

Returns:

  • (Boolean)


39
40
41
# File 'lib/future.rb', line 39

def respond_to?(method)
  :force.equal?(method) || :__force__.equal?(method) || __force__.respond_to?(method)
end