Class: Ryo::Function

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

Overview

The Ryo::Function class represents a Ryo function. The class is usually not used directly but through Ryo.fn. A Ryo function has a special relationship with Ryo objects: when a Ryo function is assigned as a property on a Ryo object - the “self” of the function becomes bound to the Ryo object.

Direct Known Subclasses

Memo

Instance Method Summary collapse

Constructor Details

#initialize(&body) ⇒ Ryo::Function

Returns an instance of Ryo::Function.

Parameters:

  • body (Proc)

    The body of the function as a block.



17
18
19
20
21
# File 'lib/ryo/function.rb', line 17

def initialize(&body)
  @body = body
  @ryo = nil
  @to_proc = nil
end

Instance Method Details

#receiver<Ryo::Object, Ryo::BasicObject> Also known as: self

Returns the object that the function’s “self” is bound to.

Returns:



26
27
28
# File 'lib/ryo/function.rb', line 26

def receiver
  @ryo
end

#bind!(ryo) ⇒ nil

Change the receiver (self) of the function.

Parameters:

Returns:

  • (nil)


38
39
40
41
# File 'lib/ryo/function.rb', line 38

def bind!(ryo)
  @ryo = ryo
  @to_proc = nil
end

#callObject

Call the function.



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

def call(...)
  to_proc.call(...)
end

#to_procProc

Returns the function as a lambda bound to #receiver.

Returns:

  • (Proc)

    Returns the function as a lambda bound to #receiver.



52
53
54
# File 'lib/ryo/function.rb', line 52

def to_proc
  @to_proc ||= lambda!(@body)
end