Class: Ryo::Function
- Inherits:
-
Object
- Object
- Ryo::Function
- 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
Instance Method Summary collapse
-
#initialize(&body) ⇒ Ryo::Function
constructor
Returns an instance of Ryo::Function.
-
#receiver ⇒ <Ryo::Object, Ryo::BasicObject>
(also: #self)
Returns the object that the function’s “self” is bound to.
-
#bind!(ryo) ⇒ nil
Change the receiver (self) of the function.
-
#call ⇒ Object
Call the function.
-
#to_proc ⇒ Proc
Returns the function as a lambda bound to #receiver.
Constructor Details
#initialize(&body) ⇒ Ryo::Function
Returns an instance of Ryo::Function.
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.
26 27 28 |
# File 'lib/ryo/function.rb', line 26 def receiver @ryo end |
#bind!(ryo) ⇒ nil
Change the receiver (self) of the function.
38 39 40 41 |
# File 'lib/ryo/function.rb', line 38 def bind!(ryo) @ryo = ryo @to_proc = nil end |
#call ⇒ Object
Call the function.
45 46 47 |
# File 'lib/ryo/function.rb', line 45 def call(...) to_proc.call(...) end |
#to_proc ⇒ 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 |