Class: Zoidberg::Future

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

Overview

Perform action and fetch result in the future

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize { ... } ⇒ self

Create a new instance

Yields:

  • block to execute



14
15
16
# File 'lib/zoidberg/future.rb', line 14

def initialize(&block)
  @thread = Thread.new(&block)
end

Instance Attribute Details

#threadThread (readonly)

Returns underlying thread running task.

Returns:

  • (Thread)

    underlying thread running task



8
9
10
# File 'lib/zoidberg/future.rb', line 8

def thread
  @thread
end

Instance Method Details

#available?TrueClass, FalseClass

Check if value is available

Returns:

  • (TrueClass, FalseClass)


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

def available?
  !thread.alive?
end

#valueObject

Returns result value.

Returns:

  • (Object)

    result value



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

def value
  unless(@value)
    @value = @thread.value
  end
  @value
end