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

Create a new future

Yields:

  • The block to evaluate optimistically



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

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



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

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

Instance Method Details

#__force__Any Also known as: force

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

Returns:

  • (Any)


30
31
32
33
# File 'lib/future.rb', line 30

def __force__
  @thread.join
  @promise
end

#respond_to?(method) ⇒ true, false

Does this promise support the given method?

Parameters:

  • (Symbol)

Returns:

  • (true, false)


41
42
43
# File 'lib/future.rb', line 41

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