Class: Fr::Thunk

Inherits:
BasicObject
Defined in:
lib/fr/thunk.rb

Overview

Beware, there’s no way to make a thunk value 100% compatible with the value it computes. For instance,

String === "a"            #=> true
String === thunk { "a" }  #=> false

"a" == "a"                #=> true
"a" == thunk { "a" }      #=> false

x = "abc"
y = thunk { x }

x.equal?(x) #=> true
y.equal?(y) #=> true
x.equal?(y) #=> false
y.equal?(x) #=> false

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ Thunk

Returns a new instance of Thunk.



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

def initialize(block)
  unless block.arity.zero?
    raise ArgumentError,
      "block must have zero arity"
  end

  @block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



57
58
59
60
61
62
63
64
# File 'lib/fr/thunk.rb', line 57

def method_missing(name, *args, &block)
  unless defined? @value
    @value = @block.call
    @block = nil
  end

  @value.__send__(name, *args, &block)
end

Instance Method Details

#thunk?Boolean

Returns:

  • (Boolean)


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

def thunk?
  true
end