Class: Piggly::Util::Thunk

Inherits:
BlankSlate show all
Defined in:
lib/piggly/util/thunk.rb

Overview

Wraps a computation and delays its evaluation until a message is sent to it. Computation can be forced by calling ‘force!`

Instance Method Summary collapse

Methods inherited from BlankSlate

find_hidden_method, hide, reveal

Constructor Details

#initialize(&block) ⇒ Thunk

Returns a new instance of Thunk.



17
18
19
# File 'lib/piggly/util/thunk.rb', line 17

def initialize(&block)
  @block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



34
35
36
# File 'lib/piggly/util/thunk.rb', line 34

def method_missing(name, *args, &block)
  force!.send(name, *args, &block)
end

Instance Method Details

#force!Object



21
22
23
24
25
26
27
28
# File 'lib/piggly/util/thunk.rb', line 21

def force!
  unless @block.nil?
    @value = @block.call
    @block = nil
  end

  @value
end

#thunk?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/piggly/util/thunk.rb', line 30

def thunk?
  true
end